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?
myDataMUST be an Object, it cannot be an Array.