3

I have an array of objects being returned after an API request. It takes the form:

[{
    id: 12,
    slug: '050e8e8db93ae08ed46cd57017ef9617',
    name: 'Test Badge'
}, {
    id: 13,
    slug: '78ed09b1f2aae29ab30915ac9f167bfa',
    name: 'Test Badge'
}]

I need to test for a value: x on the key: slug, but my array of objects is hundreds of objects long. Is there a way to test for a value without looping through all the objects manually?

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Peachey_A
  • 129
  • 1
  • 2
  • 8
  • 6
    No there isn't. However, if you have to perform this test multiple times, build a `slug -> object` map so that you can simply lookup `slug`. – Felix Kling Jan 12 '15 at 17:10
  • 5
    Are the objects arriving in any particular order, by slug? If they are, you might be able to perform a binary search. If not you don't have a choice, just loop through them manually. – La-comadreja Jan 12 '15 at 17:10
  • La-comadreja -- They return in order of id, not slug. – Peachey_A Jan 12 '15 at 17:26
  • possible duplicate of [best way to get a specific object from and array without looping](http://stackoverflow.com/q/15411337/1048572) – Bergi Jan 12 '15 at 17:36

3 Answers3

3

Well - somehow you have to loop. But at least JavaScript is doin the job for you:

var arr = [
    {
        id: 12,
        slug: '050e8e8db93ae08ed46cd57017ef9617',
        name: 'Test Badge'
    },
    {
        id: 13,
        slug: '78ed09b1f2aae29ab30915ac9f167bfa',
        name: 'Test Badge'
    }
];

var arrWithValue = arr.filter(function(el){ return el.slug === value; });

arrWithValue contains only the elements of the array with the correct value.

When you have to access these data very often it would be better to loop one time and save every object using the slug as key.

var sortedObj = {};
for(var i = 0, len = arr.length; i < len; ++i){
    sortedObj[arr[i].slug] = arr[i];
}

// access the object with sortedObj['someSlug']
Fuzzyma
  • 7,619
  • 6
  • 28
  • 60
  • 1
    Good answer overall. I'd suggest [not using `for-in` on arrays](http://stackoverflow.com/questions/500504/), though. – Scott Sauyet Jan 12 '15 at 17:24
0

Question: do you need filter or test array for value? If test, then it is better to use some() function.

Update: According jsPerf http://jsperf.com/json-stringify-vs-array-loop some() faster in 800 times then filter() and 5000 then JSON.stringify.

    var data = [{
        id: 12,
        slug: '050e8e8db93ae08ed46cd57017ef9617',
        name: 'Test Badge'
    }, {
        id: 13,
        slug: '78ed09b1f2aae29ab30915ac9f167bfa',
        name: 'Test Badge'
    }];

    function test(x) {
        return data.some(function(d){return x==d.slug});
    };

    console.log(test('78ed09b1f2aae29ab30915ac9f167bfa'))
agershun
  • 4,077
  • 38
  • 41
  • the function [`.some()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) will still iterate through the array. – Spencer Wieczorek Jan 12 '15 at 17:16
  • According jsPerf http://jsperf.com/json-stringify-vs-array-loop some() faster in 800 times then filter() and 5000 then JSON.stringify. – agershun Jan 12 '15 at 17:34
0

Another "non-iterating" option (it is possible here because your key is very specific), but this is slowest because JSON.stringify is slow operation:

function test(x) {
    return JSON.stringify(data).indeхOf(x) > -1;
}
agershun
  • 4,077
  • 38
  • 41