Array of arrays in Javascript

Hi there,
I want to construct an array of array.   Assuming, for example, I have the following data.
I want to group them based on ID.  

[{Name:"xyz", ID:123, Price:3:00, Brand:"abc"},
{Name:"aaa", ID:123, Price:3:50, Brand:"bbb"},
{Name:"xyz", ID:123, Price:3:00, Brand:"ccc"},
{Name:"aaa", ID:223, Price:3:50, Brand:"ddd"},
{Name:"xyz", ID:223, Price:3:00, Brand:"ccc"},
{Name:"aaa", ID:333, Price:3:50, Brand:"eee"},
{Name:"xyz", ID:333, Price:3:00, Brand:"fff"},
{Name:"aaa", ID:333, Price:3:50, Brand:"sss"}]

So that the end result would be:
IDArray[0] =
[{Name:"xyz", ID:123, Price:3:00, Brand:"abc"},
{Name:"aaa", ID:123, Price:3:50, Brand:"bbb"},
{Name:"xyz", ID:123, Price:3:00, Brand:"ccc"}]

IDArray[1] =
[{Name:"aaa", ID:223, Price:3:50, Brand:"ddd"},
{Name:"xyz", ID:223, Price:3:00, Brand:"ccc"}]

IDArray[2] =
[{Name:"aaa", ID:333, Price:3:50, Brand:"eee"},
{Name:"xyz", ID:333, Price:3:00, Brand:"fff"},
{Name:"aaa", ID:333, Price:3:50, Brand:"sss"}]


So, basically the algorithm would be something like:
go through the data and for each entry, check if the ID is in the IDArray, if it exists insert the entry to the sub array.  If ID doesn't exist insert the ID to the IDArray and insert the entry to sub array.
How can I do this?  Thanks.
ambuliAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Loop through the first array and push just the id's to a new array.  If the id is already in the new array, then do add it.

Finally loop through your new array of unique id's to create each new array that contains all the data only if the id matches.
Julian HansenCommented:
This is one way of doing it
<script type="text/javascript">
// THE DATA
var data= [{Name:"xyz", ID:123, Price:3.00, Brand:"abc"},
{Name:"aaa", ID:123, Price:3.50, Brand:"bbb"},
{Name:"xyz", ID:333, Price:3.00, Brand:"fff"},
{Name:"aaa", ID:223, Price:3.50, Brand:"ddd"},
{Name:"xyz", ID:223, Price:3.00, Brand:"ccc"},
{Name:"aaa", ID:333, Price:3.50, Brand:"eee"},
{Name:"xyz", ID:123, Price:3.00, Brand:"ccc"},
{Name:"aaa", ID:333, Price:3.50, Brand:"sss"}];

// SORT FUNCTION
// SORTS IN ID ORDER
function sortCallback(a, b)
{
  return a.ID - b.ID;
}

// SORT OUR DATA SO THE RECORDS ARE IN ID ORDER
data.sort(sortCallback);

// RESULT ARRAY
var res = new Array();

// TEMPORARY ARRAY
temp = [];

// STORES THE ID WE ARE CURRENTLY PROCESSING
id = 0;

// FOR EACH KEY IN ARRAY
for (key in data) {
  // IS THIS RECORD DIFFERENT FROM THE PREVIOUS ONE
  if (id != data[key].ID) {
    // IF ID IS 0 THEN THIS IS THE FIRST ITERATION SO IGNORE 
    // THE FACT THEY ARE DIFFERENT
    if (id > 0){
      // IF ID IS SET THEN WE HAVE FOUND A DIFFERENT ID 
      // ADD TEMP ARRAY TO RESULT ARRAY
        res.push(temp);
        // REST THE TEMP ARRAY
        temp = [];
    }

    // SET id TO CURRENT ID - WHEN THIS DOES NOT MATCH THE
    // ID IN THE CURRENT RECORD WE HAVE FOUND A NEW ID BATCH
    id = data[key].ID;
  }

  // ADD THE CURRENT RECORD TO THE TEMP ARRAY
  temp.push(data[key]);
}

// FINALLY ADD THE LAST BATCH TO THE RESULT ARRAY
res.push(temp);
console.log(res);
</script>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ambuliAuthor Commented:
Excellent! Thank you
Julian HansenCommented:
You are welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.