Let's say I have a JSON array 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 an array of all the names that have the website value equal to google.
Firstly, to filter the JSON array to contain only entries where the website is google, I have this:
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. 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.
I have a JSONDo you have something in JSON or an object? They're pretty different thingselement.websitewith something truthy, so all items will pass the filter test.data_filter.bodyto be? You just loggeddata_filteras an array.