How to find value in multidimensional object / array in Javascript?

I have a multidimensional object (this is basically an array):

Object = { 1 : { name : bob , dinner : pizza }, 2 : { name : john , dinner : sushi }, 3 : { name : larry, dinner : hummus } } 

I want to be able to search for an object / array where the key is "dinner" and see if it matches with "sushi".

I know jQuery has $ .inArray, but it does not work on multidimensional arrays. Or maybe I'm wrong. indexOf also only works on one level of the array.

Is there a function or existing code for this?

+81
javascript jquery arrays search
Mar 03 2018-11-11T00:
source share
7 answers

If you have an array like

 var people = [ { "name": "bob", "dinner": "pizza" }, { "name": "john", "dinner": "sushi" }, { "name": "larry", "dinner": "hummus" } ]; 

You can use the filter method of an Array object:

 people.filter(function (person) { return person.dinner == "sushi" }); // => [{ "name": "john", "dinner": "sushi" }] 

In newer JavaScript implementations, you can use a function expression:

 people.filter(p => p.dinner == "sushi") // => [{ "name": "john", "dinner": "sushi" }] 



You can search for people with "dinner": "sushi" using map

 people.map(function (person) { if (person.dinner == "sushi") { return person } else { return null } }); // => [null, { "name": "john", "dinner": "sushi" }, null] 

or reduce

 people.reduce(function (sushiPeople, person) { if (person.dinner == "sushi") { return sushiPeople.concat(person); } else { return sushiPeople } }, []); // => [{ "name": "john", "dinner": "sushi" }] 

I am sure you can generalize this to arbitrary keys and values!

+185
Mar 03 '11 at 14:05
source share

jQuery has a built-in jQuery.grep method that works similar to the ES5 filter function from @adamse Answer and should work fine in older browsers.

Using the adamse example:

 var peoples = [ { "name": "bob", "dinner": "pizza" }, { "name": "john", "dinner": "sushi" }, { "name": "larry", "dinner": "hummus" } ]; 

you can do the following

 jQuery.grep(peoples, function (person) { return person.dinner == "sushi" }); // => [{ "name": "john", "dinner": "sushi" }] 
+17
Mar 25 '13 at 18:32
source share

If you intend to frequently perform this search, consider changing the format of your property, so dinner is actually the key. This is similar to assigning a primary cluster key in a database table. So for example:

 Obj = { 'pizza' : { 'name' : 'bob' }, 'sushi' : { 'name' : 'john' } } 

Now you can easily access it as follows: Object['sushi']['name']

Or, if the object is really so simple (just the "name" in the object), you can simply change it to:

 Obj = { 'pizza' : 'bob', 'sushi' : 'john' } 

And then enter it like this: Object['sushi'] .

Obviously, this is not always possible or in your interests to restructure your data object like this, but the fact is that it is best to answer the question whether your data object is structured in the best way. Creating such a key can be faster and create cleaner code.

+10
Mar 08 '13 at 17:12
source share
 var getKeyByDinner = function(obj, dinner) { var returnKey = -1; $.each(obj, function(key, info) { if (info.dinner == dinner) { returnKey = key; return false; }; }); return returnKey; } 

jsFiddle .

So far, -1 never a valid key.

+8
Mar 03 2018-11-11T00:
source share

You can find the object in the array using Alasql :

 var data = [ { name : "bob" , dinner : "pizza" }, { name : "john" , dinner : "sushi" }, { name : "larry", dinner : "hummus" } ]; var res = alasql('SELECT * FROM ? WHERE dinner="sushi"',[data]); 

Try this example in jsFiddle .

+3
Dec 21 '14 at 20:48
source share

You can use a simple for loop:

 for (prop in Obj){ if (Obj[prop]['dinner'] === 'sushi'){ // Do stuff with found object. Eg put it into an array: arrFoo.push(Obj[prop]); } } 

The following script example puts all objects containing dinner:sushi into an array:

https://jsfiddle.net/3asvkLn6/1/

+1
Jul 07 '15 at 1:10
source share

There are already a lot of good answers here, so why not, use a library like lodash or underscore :)

 obj = { 1 : { name : 'bob' , dinner : 'pizza' }, 2 : { name : 'john' , dinner : 'sushi' }, 3 : { name : 'larry', dinner : 'hummus' } } _.where(obj, {dinner: 'pizza'}) >> [{"name":"bob","dinner":"pizza"}] 
+1
Jul 30 '15 at 1:19
source share



All Articles