0

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?

2
  • You will need arrays for each object index if you want to store multiple items there Commented May 21, 2014 at 9:03
  • Please read my answer and my answer to your comment instead of editing. myData MUST be an Object, it cannot be an Array. Commented May 21, 2014 at 9:21

2 Answers 2

1

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"}
Sign up to request clarification or add additional context in comments.

Comments

0

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"}
    ]
};

4 Comments

the result is something like this: c[undefined × 1, Array[3] , Array[1] , undefined × 1, Array[3] ]c any idea how to remove all the undefined?
@Dima Did you read the second paragraph of my answer ? myData MUST be an object, it cannot be an array.
@sebcap26 is right. If myData is an Array, there will be many position's values are not set, and be undefined. It has to be a 'Object'. You have to understand that for JavaScript Arrays, a = []; a[100] =1;, the length of a is 100, not 1.
@sebcap26, Thank you... my mistake was that I set myData as Array and not Object.

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.