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 }
source share