Creating an array out of a specific JSON Key-Value Pair

  • A+
Category:Languages

Let's say I have a JSON like this

[{"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 the name in an array, when the website is google. To filter the JSON with the website is google part , I have this

var data_filter = data.filter( element => element.website =="google"); console.log(data_filter); 

which gives a result like

[{"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. I tried doing this

let new_array = [];   new_array.push(data_filter.body.name) 

which gives me an undefined error for 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.


You need to use double equals sign to compare == instead of single =. When it’s single, you change (assign) element.website to "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().

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().

Your code didn’t work because you try to access a property body of the filtered results. 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. 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.

Read more about filter() here.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: