0

I have a JSON object with, say, 500 objects in an array. At the moment, to find the one I want, I'm using jQuery's "each" and comparing each object's id with the id I'm looking for, like so:

var desiredID = 500;
$.each(myObj.arrayOfObjects, function(k, oneObject){
    if((oneObject.lineid * 1) === (desiredID * 1)){
        // hooray! I have found my object
    }
});

Is there a better, more efficient way? The array of objects could potentially get impressively large.

(In this instance, I'm using jQuery, but it could be vanilla javascript, too.)

8
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… , if you don't need IE support. And if you do I'm sure there's a polyfill some where. Commented Apr 7, 2020 at 22:12
  • You can achieve similar result using vanilla javascript as well: myObj.arrayOfObjects.find(oneObject => (oneObject.lineid * 1) === (desiredID *1)) Commented Apr 7, 2020 at 22:13
  • 1
    Though, from a data model standpoint, one could argue if the element have an "id", they might could be better represented as a map. Then id lookups are super easy Commented Apr 7, 2020 at 22:13
  • 1
    Side note; also not entirely sure why you are multiplying the desiredID by 1... Commented Apr 7, 2020 at 22:14
  • @Taplar I'll bet it's to convert from string to int. Commented Apr 7, 2020 at 22:15

2 Answers 2

1

with vanilla js:

const obj = arrayOfObjects.find(({ lineid }) => lineid === desiredID)

if you want to convert string to number, use this triks:

         +'1' === 1 // true
        ~~'1' === 1 // true
  Number('1') === 1 // true
parseInt('1') === 1 // true

and for the desiredID you don't have to cast, because you know it's number

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

6 Comments

for the sake of my own education: is Array.find() that much more efficient than just writing your own loop and checking against each? If so, why?
it depends on what size it is, but for the most part yes
Interesting... so up to a certain size array it is more efficient and then it stops being the faster way as the array grows? Or the opposite?
now it also depends on which search algorithm you write, if it is just 'for' is the same thing
Also keep in mind, if you do not break out of your for loop once you find what you are looking for, an each will evaluate every element even once it is found, where as a find() will stop on the first match immediately
|
0
myObj.mappedObjects = myObj.arrayOfObjects.reduce((r, e) => {
    r[+e.lineid] = e;
    return r;
}, {});

var desiredID = 500;
myObj.mappedObjects[desiredID];

Given that you mentioned possibly having 500ish elements, if there is the potential of having to look up elements repeatedly, I would suggest converting the array into a map, for quick id lookups going forward.

If the endpoint, or where ever, you are getting the original data could put it in that format to begin with, even better.

3 Comments

If you are looking for a single time in object, then 'reduce' it will consume much more than 'find'
@Chris perhaps you missed my comment about repetitions?
I'm sorry, I wasn't paying attention

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.