我有以下数组:
[
{
fileName: "test.pdf",
fileUrl: "https://test-url1.com/test.pdf",
isDeleted: true
},
{
fileName: "test.pdf",
fileUrl: "https://test-url1.com/test.pdf",
isDeleted: true
}
]
现在我需要从此数组创建新数组,其中仅包含 fileUrl 数据。
这样新数组将只有 fileUrl 节点及其所有链接。
我应该使用 map 或过滤方法,或任何其他建议。
谢谢
最佳答案
为此,您应该使用map
。
const urlArray = array.map((item: any) => item.fileUrl);
// [ "https://test-url1.com/test.pdf", "https://test-url1.com/test.pdf"]
经验法则是,如果您想更改数组的结构,请使用map
,如果您想获取数组的子集,请使用filter
。
关于angular - 使用特定节点值从现有数组创建新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75613858/