0

I have an array of ints called SelectedItems. I have another array that contains objects that's called AvailableItems and that was parsed from json; these Item objects have the properties {ContainerID, ContainerName, ItemID, ItemName}.

I want to convert SelectedItems from an array of ints to an array of Items where each ItemID is replaced with the Item object that corresponds to the ItemID in AvailableItems. Each ItemID in SelectedItems is unique.

I started with 2 loops: one that loops through each element of SelectedItems but then I find myself looping through AvailableItems each time to find the corresponding ItemID with the object keys I need to copy into SelectedItems.

So basically I built a seemingly very inefficient loop. I was wondering if there was a better way to do it by avoiding a repeated loop inside a loop?

Sample data:

For AvailableItems, you have

{
  ContainerID: i, 
  ContainerName: 'SomeName', 
  ItemID: j, 
  ItemName: 'SomeOtherName'
}

with may be 1,000 objects and then SelectedItems is array of ints

[23,43,64,34...]

Thanks.

Sampson
  • 265,109
  • 74
  • 539
  • 565
frenchie
  • 51,731
  • 109
  • 304
  • 510
  • you could filter the second array according to the first , then return the result , with functions like Array.filter , you can check if an element is contained in the first one with Array.indexOf that should be > -1 each time the condition is true. – mpm May 01 '12 at 15:17
  • @camus: ok, your comment gave the idea on how to redo it better. I copy the AvailableItems array with jquery.extend to a new array called CandidateItems and then I loop through that array: I check to see if the ItemID of each element is in SelectedItems with $.InArray and if it's not I delete that element. At the end of the loop, I assign CandidateArray to SelectedItems and voila – frenchie May 01 '12 at 15:31

2 Answers2

2

You could put each object in the numeric array AvailableItems at the index that matches its ItemID.

So you know that the item with an ItemID of 5 is at AvailableItems[5] instead of having to loop through and find it.

Not sure what effect this will have if you have big gaps between different ItemID values, but you can try it and see if it works well.


UPDATE:

After a quick search, and reading this answer, it looks like having gaps between your indexes will not waste a bunch of memory. However, it will affect the result of checking AvailableItems.length. If you have an array with one entry, but the index of that entry is 500, then AvailableItems.length will return 501, even though there is only one entry in the array.

As long as you don't need to use the length function, this solution should work for you.

Community
  • 1
  • 1
Travesty3
  • 14,351
  • 6
  • 61
  • 98
  • ok, I got the idea on how to redo it. I copy the AvailableItems array with jquery.extend to a new array called CandidateItems and then I loop through that array: I check to see if the ItemID of each element is in SelectedItems with InArray and if it's not I delete that element. At the end of the loop, I assign CandidateArray to SelectedItems and voila – frenchie May 01 '12 at 15:32
1

If you are able to get any arbitrary AvailableItem without looping through the whole array by just addressing it by its ID(if you have an index-based array where IDs are indexes) then you can go through the SelectedItems and check if it exists in AvailableItems, and if it does then you convert the SelecteItem into an object and add it to some temporary array for later use.

montrealist
  • 5,593
  • 12
  • 46
  • 68
  • ok, your answer gave the idea on how to redo it better. I copy the AvailableItems array with jquery.extend to a new array called CandidateItems and then I loop through that array: I check to see if the ItemID of each element is in SelectedItems with InArray and if it's not I delete that element. At the end of the loop, I assign CandidateArray to SelectedItems and voila. – frenchie May 01 '12 at 15:30