Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 302
demouser123 Avatar asked May 11 '18 07:05

demouser123


People also ask

Can a JSON key be an array?

JSON is basically a collection of name/value pairs, where the name will always be a string and values can be a string (in double quotes), a number, a boolean (true or false), null, an object, or an array.

How do you convert JSON to an array?

Approach 1: First convert the JSON string to the JavaScript object using JSON. Parse() method and then take out the values of the object and push them into the array using push() method.

How do you create an array of objects in JSON?

Create a JSON array by instantiating the JSONArray class and add, elements to the created array using the add() method of the JSONArray class.

What is [] and {} in JSON?

' { } ' used for Object and ' [] ' is used for Array in json.


2 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.

like image 170
dodov Avatar answered Sep 27 '22 21:09

dodov


Use map after filter (also correct = with === )

var data_filter = data.filter( element => element.website === "google").map( s => s.name );

Or if you don't want to iterate twice, use reduce

data.reduce( (a, c) => {
   c.website === "google" ? a.push( c.name ) : "";
   return a; //return accumulator
} , []); //initialize accumulator
like image 45
gurvinder372 Avatar answered Sep 27 '22 20:09

gurvinder372