假设我有一个像这样的JSON数组
[{"name":"Lenovo Thinkpad 41A4298","website":"google"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"},
{"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"},
{"name":"Lenovo Thinkpad 41A424448","website":"google"},
{"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"},
{"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}]
我希望得到name
值等于website
的所有google
数组。
首先,要过滤JSON数组以仅包含website
为google
的条目,我有:
var data_filter = data.filter( element => element.website =="google");
console.log(data_filter);
得出以下结果:
[{"name":"Lenovo Thinkpad 41A4298","website":"google"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"},
{"name":"Lenovo Thinkpad 41A424448","website":"google"}]
我需要做什么才能将name
放在一个单独的数组中。我试过这样做:
let new_array = [];
new_array.push(data_filter.body.name)
给了我name
的未定义错误。我也尝试过:
new_array.push(data_filter.name)
new_array.push(data_filter.body[0].name)
但这些方法都不奏效。我在这里缺少什么?
FYI - 此SO post中提到了JSON数据和过滤方法 - OP和答案的信用。
答案 0 :(得分:9)
您需要使用双等号来比较==
而不是单=
。如果是单身,则将(分配)element.website
更改为"google"
。该表达式的结果是您设置的值,即"google"
,它是一个真值,因此所有元素都通过了filter()
的测试。
var data = [{"name":"Lenovo Thinkpad 41A4298","website":"google"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"},
{"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"},
{"name":"Lenovo Thinkpad 41A424448","website":"google"},
{"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"},
{"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}];
var data_filter = data.filter( element => element.website == "google");
var names = data_filter.map(function (elem) {
return elem.name;
});
console.log(names);

要在过滤结果后获取名称,请使用map()
。
您的代码无效,因为您尝试访问已过滤结果的属性body
。过滤结果包含原始结果的数组,但仅包含通过测试的条目。由于您的原始条目没有body
属性,因此过滤后的结果也没有。而且,您尝试了data_filter.body
,它永远不会存在,因为data_filter
将始终是一个数组,并且数组没有body
属性。
详细了解filter()
here。
答案 1 :(得分:4)
您可以将map
方法与filter
结合使用,并为每个方法传递回调提供的功能。
let data = [{"name":"Lenovo Thinkpad 41A4298","website":"google"}, {"name":"Lenovo Thinkpad 41A2222","website":"google"}, {"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"}, {"name":"Lenovo Thinkpad 41A424448","website":"google"}, {"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"}, {"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"}, {"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"}, {"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}]
names = data.filter(function(item){
return item.website == 'google';
}).map(function(item){
return item.name;
});
console.log(names)
另一种方法是使用arrow
函数和 destructure 参数。
let data = [{"name":"Lenovo Thinkpad 41A4298","website":"google"}, {"name":"Lenovo Thinkpad 41A2222","website":"google"}, {"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"}, {"name":"Lenovo Thinkpad 41A424448","website":"google"}, {"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"}, {"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"}, {"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"}, {"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}]
names = data.filter(({website}) => website == 'google').map(({name}) => name);
console.log(names)
答案 2 :(得分:1)
根据条件使用reduce
推送到另一个阵列:
const data = [{"name":"Lenovo Thinkpad 41A4298","website":"google"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"},
{"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"},
{"name":"Lenovo Thinkpad 41A424448","website":"google"},
{"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"},
{"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}];
const nameArr = data.reduce((nameArr, { name, website }) => {
if (website === 'google') nameArr.push(name);
return nameArr;
}, []);
console.log(nameArr);