I have encountered a challenge while attempting to create a new array where the key is the index of the original array.
var array = [
{"tom": "red", "part":"green", "brow_id":45},
{"tom": "red", "part":"yellow", "brow_id":1},
{"tom": "red", "part":"yellow", "brow_id":2},
{"tom": "maroon", "part":"cyan", "brow_id":45}
];
var newarray = {};
array.forEach(function(elem) {
newarray[elem.brow_id] = elem;
});
The resulting array looks like this
45: {"tom": "red", "part":"green", "brow_id":45},
1: {"tom": "red", "part":"yellow", "brow_id":1},
2: {"tom": "red", "part":"yellow", "brow_id":2},
However, I aim for it to include all the IDs from the original array in this format
45: [{"tom": "red", "part":"green", "brow_id":45},{"tom": "maroon", "part":"yellow", "brow_id":45}]
1: {"tom": "red", "part":"yellow", "brow_id":1},
2: {"tom": "red", "part":"yellow", "brow_id":2},
What could be causing this issue?