[英]Creating an array out of a specific JSON Key-Value Pair
Let's say I have a JSON array like this 假设我有一个像这样的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"}]
I want to get an array of all the name
s that have the website
value equal to google
. 我想获得一个
website
值等于google
的所有name
的数组。
Firstly, to filter the JSON array to contain only entries where the website
is google
, I have this: 首先,要过滤JSON数组以仅包含
website
为google
条目,我有:
var data_filter = data.filter( element => element.website =="google");
console.log(data_filter);
which gives the following result: 得出以下结果:
[{"name":"Lenovo Thinkpad 41A4298","website":"google"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"},
{"name":"Lenovo Thinkpad 41A424448","website":"google"}]
What do I need to do next to get the name
in a separate array. 接下来我需要做什么才能在单独的数组中获取
name
。 I tried doing this: 我试过这样做:
let new_array = [];
new_array.push(data_filter.body.name)
which gives me an undefined error for name
. 这给了我一个
name
未定义的错误。 I also tried: 我也尝试过:
new_array.push(data_filter.name)
new_array.push(data_filter.body[0].name)
But none of the approaches work. 但这些方法都不起作用。 What am I missing here?
我在这里错过了什么?
FYI - JSON data and Filter approach is mentioned in this SO post - credits to the OP and answers. 仅供参考 - JSON数据和过滤方法在此SO 帖子中提及 - OP和答案。
You need to use double equals sign to compare ==
instead of single =
. 您需要使用双等号来比较
==
而不是single =
。 When it's single, you change (assign) element.website
to "google"
. 当它的单,你改变 (分配)
element.website
以"google"
。 The result of that expression is the value you set, which is "google"
and it is a truthy value and therefore all elements pass the test of filter()
. 该表达式的结果是您设置的值,即
"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);
To get the names after you've filtered the results, use map()
. 要在过滤结果后获取名称,请使用
map()
。
Your code didn't work because you try to access a property body
of the filtered results. 您的代码无效,因为您尝试访问过滤结果的属性
body
。 The filtered results consist of an array of your original results, but only the entries that pass the test. 过滤结果包含原始结果的数组,但仅包含通过测试的条目。 Since your original entries don't have a
body
property, the filtered results won't have it either. 由于您的原始条目没有
body
属性,因此过滤后的结果也不会有。 And also, you tried data_filter.body
which will never exist because data_filter
will always be an Array and arrays don't have a body
property. 而且,您尝试了
data_filter.body
,它永远不会存在,因为data_filter
将始终是一个数组,并且数组没有body
属性。
You can use map
method in combination with filter
and pass callback provided functions for each of them. 您可以将
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)
Another approach is to use arrow
functions and destructure the arguments. 另一种方法是使用
arrow
函数并对参数进行解构 。
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)
Use map
after filter
(also correct =
with ===
) filter
后使用map
(也正确=
带===
)
var data_filter = data.filter( element => element.website === "google").map( s => s.name );
Or if you don't want to iterate twice, use reduce
或者,如果您不想迭代两次,请使用
reduce
data.reduce( (a, c) => {
c.website === "google" ? a.push( c.name ) : "";
return a; //return accumulator
} , []); //initialize accumulator
Use reduce
to push to another array based on a condition: 使用
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);
You can simply map over the object array and IF item.website === 'google', push item.name into an array. 您可以简单地映射对象数组和IF item.website ==='google',将item.name推送到数组中。 See the working solution below...
请参阅下面的工作解决方案
let theJSON = [ {"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"} ] let nameArray = [] theJSON.map((item) => { if (item.website === 'google') { nameArray.push(item.name) } }) console.log(nameArray)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.