I am currently using JavaScript ES6 to create the following output object from a .map array method.
const indexAndValue = (arr) => {
return arr.map((elem, index) => {
return { index, elem }
});
}
indexAndValue([22, 33, 44, 55])
The output code is:
[ { index: 0, elem: 22 },
{ index: 1, elem: 33 },
{ index: 2, elem: 44 },
{ index: 3, elem: 55 } ]
How do I get the same output without using the {} shortcut? I tried the following code:
const indexAndValue = (arr) => {
let obj = {};
return arr.map((elem, index) => {
return obj[index] = elem;
});
}
indexAndValue([22, 33, 44, 55])
This code returns incorrectly:
[ 22, 33, 44, 55 ]