1

Please note that the following scenario is for the demonstration purposes only.

Lets assume I have a following array of object:

var obj = [{
    id: 4345345345,
    cat: [{
        id: 1,
        cat: "test1"
    }, {
        id: 2,
        cat: "test2"
    }]
}, {
    id: 3453453421,
    cat: [{
        id: 1,
    }, {
        id: 2,
    }]
}];

My goal is to :

  • Find an object within an array with #id 4345345345, add property selected : true to it
  • Then within this object with #id 4345345345, find cat with #id 2, add property selected : true to it


The below works, however should my array have 1000+ objects it's feels somehow wasteful, can you please suggest any cleaner/clever solution ( possible using underscore)?

for (var i = 0; i < obj.length; i++) {
    var parent = obj[i];
    if (parent.id === 4345345345) {

        parent.selected = true;

        for (var j = 0; j < parent.cat.length; j++) {
            var sub = parent.cat[j];
            if(sub.id === 2) {
                sub.selected = true;
            }
        };



    }
};
11
  • 3
    If you've only got 1,000 elements, I wouldn't worry about it. But if you really want to be anal about performance.... 1) Can you guarantee that only 1 element will be matched? If so, break out the loop once you've matched an element. 2) You could sort your array in order of ID, then use binary search rather than linear. Commented Aug 11, 2014 at 10:46
  • Seems better suited for codereview.SE Commented Aug 11, 2014 at 10:46
  • 1
    It feels wasteful because you're doing a brute-force search of a large array to see if the id matches a specific value. If you knew the array were going to be in order (e.g. the object with id 122 were always the nth member of the array) you could just do obj[n] to get it instead of using the loop. Commented Aug 11, 2014 at 10:47
  • 2
    Try to insert them in sort order (id), you should then be able to find items very fast even if there are millions, using binary search or such. This is of course worth it mostly if you seek more than insert. Commented Aug 11, 2014 at 10:47
  • 1
    @Iladarsda Why? Check out what it means :) It is just an algorithm Commented Aug 11, 2014 at 11:34

1 Answer 1

1

Here are a few approaches I can think of

1) change your data structure to use the id's as the key. ie.

4345345345 : cat: { 1 :{ cat: "test1" }, 2 : { cat: "test2" }}

2) Alternatively you can create a temporary lookup table based on the id to directly look the object actual objects; obviously you would only create the look up table one time, or whenever the data changes. This will bring your runtime from O(n) to O(1).

Sign up to request clarification or add additional context in comments.

1 Comment

How to do #1 ? I need this as well!

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.