-1

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
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Pezh
  • 65
  • 5

2 Answers2

0

You can simplify that with some higher order functions:

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

const result = Object.keys(dataEnum)
  .filter(d => dataEnum[d].Number == "3")
  .map(d => dataEnum[d].Name + " and number " + dataEnum[d].Number + " was read from the array");

console.log(result);

Returns ["C and number 3 was read from the array"]

R. Tero
  • 21
  • 1
  • Thanks Rostero. Your suggestion works perfectly. Though I need to transfer the item into an array to be retrieved at a later time and in the actual code there will be more elements pushed in the array as a list of items that will be read from at a later time – Pezh Jun 30 '17 at 19:06
0

You're printing dataEnum[y], but y is the index into dataArray, not dataEnum. The value of the element in dataArray[y] is the index into dataEnum, so you should print dataEnum[dataArray[y]].

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 " + dataArray[y] + " with name " + dataEnum[dataArray[y]].Name + " and number " + dataEnum[dataArray[y]].Number + " was read from the array");
  // Results in:
  // Object 0 with name A and number 1 was read from the array
}
Barmar
  • 741,623
  • 53
  • 500
  • 612