I am trying to build an array of objects. I want the key to be a value of a variable but my code is inserting a literal. Thank you for any assistance.
I want to generate
resArr [ { 0: { ‘0’: 3, ‘1’: 2, ‘2’: 8 } },
{ 1: { ‘0’: 4, ‘1’: 4, ‘2’: 5 } },
{ 2: { ‘0’: 3, ‘1’: 7, ‘2’: 2 } } ]
but I get
resArr [ { pos: { ‘0’: 3, ‘1’: 2, ‘2’: 8 } },
{ pos: { ‘0’: 4, ‘1’: 4, ‘2’: 5 } },
{ pos: { ‘0’: 3, ‘1’: 7, ‘2’: 2 } } ]
code:
let x = [ {0:3, 1:2, 2:8}, {0:4, 1:4, 2:5}, {0:3, 1:7, 2:2} ];
let resArr = [];
let pos = 0;
let oby;
x.forEach(function(ele) {
oby = {pos : ele};
console.log(ele);
resArr.push(oby);
pos++;
});
console.log("resArr", resArr);