Can I get the benefits of both, objects and arrays in JavaScript? -


I have a few pieces of data tagged with distributed IDs in different collections.

I need to access this data directly with ID, but I need a loop against the data.

  var listOfPartA = {34323: {foo: 7, times: 123}, 6435: {foo: 2, times: 163}, 3123: {foo: 3, times: 223} ...}; Var listOfPartB = {34523: {falcon: 1}, 6435: {falcon: 4}, 3123: {falcon: 6}, ...};  

If I want to get specific data retrieval for ID, then it is fast, but if I try to loop, then all the data retrofits slows down.

  var listOfPartA = [{Id: 34523, foo: 7, times: 123}, {id: 6435, foo: 2, bar: 163}, {id: 3123, foo: 3 , Bar: 223}, ...]; Var listOfPartB = [{id: 34523, falcon: 1}, {id: 6435, baz: 4}, {id: 3123, baz: 6}, ...];  

If I want to get specific data retrieval for these items ID, then it is slow because I have to manually search for them, but if I loop all the databases I'm so fast.

Can not I walk fast and walk fast?

Pre-indexing of your array:

  var listOfPartA = [ {Id: 34523, foo: 7 times: 123}, ...]; Var lookupPartA = {}; ListOfPartA.forEach (Task (X, I) {lookupPartA [x.id] = i;});  

Now, you can loop quickly with the listOfPartA array, but you can quickly find it too:

  ListOfPartA [lookupPartA [34523]]  

Comments