Best way to find an object in an array instead of a loop?

Example

Link: http://jsfiddle.net/ewBGt/

var test = [{ "name": "John Doo" }, { "name": "Foo Bar" }] var find = 'John Doo' console.log(test.indexOf(find)) // output: -1 console.log(test[find]) // output: undefined $.each(test, function(index, object) { if(test[index].name === find) console.log(test[index]) // problem: this way is slow }) 

Problem

In the above example, I have an array with objects. I need to find an object with name = 'John Doo'

My .each works, but this part will run 100 times, and the test will contain a lot more objects. Therefore, I think this path will be slow.

indexOf() will not work because I cannot find the name in the object.

Question

How can I find an object with name = 'John Doo' in my current array?

+6
source share
5 answers

jQuery $.grep (or another filtering function) is not an optimal solution.

The $.grep function will scroll through all elements of the array, even if the desired object is already found during the loop.

From the jQuery grep documentation:

The $ .grep () method removes items from the array as necessary so that all other items pass the provided test. A test is a function that is passed an array element and the index of an element in an array. Only if the test returns true, the element will be in the array of results.

If your array is not sorted, nothing can beat this:

 var getObjectByName = function(name, array) { // (!) Cache the array length in a variable for (var i = 0, len = test.length; i < len; i++) { if (test[i].name === name) return test[i]; // Return as soon as the object is found } return null; // The searched object was not found } 
+7
source

Sometimes I did a “search cartographic object” in a similar situation. If the array itself is static, you can convert it to a map, where the array values ​​can be keys and index values ​​of the map. I accept the values ​​as unique as in your example.

Lo-Dash (www.lodash.com ) created a selection of utils for an easy cycle, etc. Check this!

Note. . But often you don’t have to worry about looping an array of 100 elements.

+7
source

If you just want to find out if there is a value, you can use the lodash includes function as follows:

 var find = 'John Doo' [{ "name": "John Doo" }, { "name": "Foo Bar" }].some(function (hash) { if (_.includes(hash, find)) return true; }); 

Documentation:

+3
source

Perhaps you should use the $.grep functionality in jQuery:

 var test = [{ "name": "John Doo" }, { "name": "Foo Bar" }] var find = 'John Doo' var found = $.grep(test, function(obj){ return obj['name'] == find; }); console.log(found); 

Fiddle: http://jsfiddle.net/ewBGt/3/

0
source

The only thing you can do is use the built-in massive methods (if any) in your preference before doing the loop yourself - filter applicable here.

But I expect JS libraries like jQuery used by sbeliv01 in his answer already check this inside (and provide a fallback solution if these array methods are not available initially) - so don't expect a significant performance boost.

0
source

All Articles