I am new to javascript. I put the following code together to get an item in an enum, push it to an array and then read it back from the array. For some reason what I push to the array is different from what I get when I read the array. I can think of some theory about how it might be happening such as when I push an element from enum to the array if that is the only element in the array, it will be interpreted as the first element in the enum when it is read back but I would appreciate a better explanation as to how this is happening: Here is my code:
var dataEnum = {
0: {Name: "A", Number: "1"},
1: {Name: "B", Number: "2"},
2: {Name: "C", Number: "3"},
3: {Name: "D", Number: "4"},
4: {Name: "E", Number: "5"}
};
var dataArray = [];
for (var x in dataEnum) {
if (dataEnum[x].Number == "3") {
dataArray.push(x);
console.log("Object " + x + " with name " + dataEnum[x].Name + " and number " + dataEnum[x].Number + " was pushed in the array");
// Results in:
// Object 2 with name C and number 3 was pushed in the array
}
}
for (var y in dataArray) {
console.log("Object " + y + " with name " + dataEnum[y].Name + " and number " + dataEnum[y].Number + " was read from the array");
// Results in:
// Object 0 with name A and number 1 was read from the array
}