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?

4
  • 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. Commented Jan 12, 2015 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. Commented Jan 12, 2015 at 17:10
  • La-comadreja -- They return in order of id, not slug. Commented Jan 12, 2015 at 17:26
  • possible duplicate of best way to get a specific object from and array without looping Commented Jan 12, 2015 at 17:36

3 Answers 3

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']
Sign up to request clarification or add additional context in comments.

1 Comment

Good answer overall. I'd suggest not using for-in on arrays, though.
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'))

2 Comments

the function .some() will still iterate through the array.
According jsPerf jsperf.com/json-stringify-vs-array-loop some() faster in 800 times then filter() and 5000 then JSON.stringify.
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.