我正在尝试为搜索结果页面创建一个名称数组
以下是json的结构:
data = {
"artists": [
{ "artistName": "a" },
{ "artistName": "b" },
{ "artistName": "c" },
{ "artistName": "d" },
{ "artistName": "e" }
]
}
到目前为止,这是我失败的尝试:
data.artists.filter(artistName)
我如何正确使用过滤方法?使用Reduce会更好一些吗?
答案 0 :(得分:1)
您必须使用map
let data = {
"artists": [
{ "artistName": "a" },
{ "artistName": "b" },
{ "artistName": "c" },
{ "artistName": "d" },
{ "artistName": "e" }
]
};
let result = data.artists.map(e => e.artistName);
console.log(result);

Doc:map