使用特定的JSON键值对创建数组

时间:2018-05-11 07:04:52

标签: javascript arrays json

假设我有一个像这样的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数组以仅包含websitegoogle的条目,我有:

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和答案的信用。

3 个答案:

答案 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);