Dima
Dima

Reputation: 443

Sorting Array by pushing objects to Array by Index

I'm parsing an XML file and building an Array of objects. I get objects with the same ID and I want to add them to the same INDEX where the other's ID are.

I tried to check by ID and .push objects to the same INDEX, but it not works.

 if (myData[object.ID] = null)
     myData[object.ID] = object;
  else {
      myData[object.ID].push(object);
   }

Any ideas how can I make something like this?

    myData[0] = {ID: "133", text: "car 1"}
    myData[1] = {ID: "143433", text: "car 34"}
    myData[2] = {ID: "55", text: "car 12"}
    myData[3] = {ID: "66", text: "car51"}, {ID: "66", text: "car56"}, {ID: "66", text: "car 323"}
    myData[4] = {ID: "32323", text: "car132"}

EDIT:

after tried @sebcap26, @antur123 answers:

if (myData[object.ID] = null) {
    myData[object.ID] = [];
}

myData[object.ID].push(object);

I get as a result something like this:

myData[undefined × 1,Array[3], Array[1], undefined × 1, Array[3],undefined × 1,undefined × 1,undefined × 1,undefined × 1,Array[1]]

how to remove those undefined?

Upvotes: 0

Views: 47

Answers (2)

Sebastien C.
Sebastien C.

Reputation: 4833

You must use an object (like an associative array) to use an id as a key. Each value will be an array.

You can't use an array to index it because you're using ids, not indexes. That's important, because if you try to set myData[143433] when myData is an array, it will work, but it will create an array with 143434 undefined elements, which is not really what you want to do ...

var myData = {};

if(!myData[object.ID]) {
    myData[object.ID] = [];
}
myData[object.ID].push(object);

The result will be like :

var myData = {
    "133": [
        {ID: "133", text: "car 1"}
    ],
    "143433": [
        {ID: "143433", text: "car 34"}
    ],
    "55": [
        {ID: "55", text: "car 12"}
    ],
    "66": [
        {ID: "66", text: "car51"},
        {ID: "66", text: "car56"},
        {ID: "66", text: "car 323"}
    ],
    "32323": [
        {ID: "32323", text: "car132"}
    ]
};

Upvotes: 0

German Latorre
German Latorre

Reputation: 11138

You can store an array in each index of your "myData" object like this:

if (myData[object.ID] = null) {
    myData[object.ID] = [];
}

myData[object.ID].push(object);

Resulting "myData" would be:

myData[0] = [{ID: "133", text: "car 1"}]
myData[1] = [{ID: "143433", text: "car 34"}]
myData[2] = [{ID: "55", text: "car 12"}]
myData[3] = [{ID: "66", text: "car51"}, {ID: "66", text: "car56"}, {ID: "66", text: "car 323"}]
myData[4] = [{ID: "32323", text: "car132"}]

If you want to store an array only if there's more than one occurrence, and just the object in any other case, the code would be like this:

if (myData[object.ID] = null) {
    // If index is empty, store object
    myData[object.ID] = object;
} else if (typeof(myData[object.ID]) === 'array') {
    // If index has an array, add object to array
    myData[object.ID].push(object);
} else {
    // If index has an object, create an array and store
    // both "old" and "new" objects
    var oldObject = myData[object.ID];
    myData[object.ID] = [];
    myData.push(oldObject);
    myData.push(object);
}

However, this leads to a weird data structure, in which you would have, for each object index, sometimes an array, sometimes an object. A bit hard to handle and work with...

Resulting "myData" in this case would be:

myData[0] = {ID: "133", text: "car 1"}
myData[1] = {ID: "143433", text: "car 34"}
myData[2] = {ID: "55", text: "car 12"}
myData[3] = [{ID: "66", text: "car51"}, {ID: "66", text: "car56"}, {ID: "66", text: "car 323"}]
myData[4] = {ID: "32323", text: "car132"}

Upvotes: 1

Related Questions