Good morning,
I’m struggling with a small problem, and I don’t find an answer for my case :
I have an array of value : let’s say
let tab = [1,2,3]
Then I have an array of json :
let values = [
{key1 : "a", key2 : "b", key3: "c"},
{key1 : "d", key2 : "e", key3: "f"},
{key1 : "g", key2 : "h", key3: "i"}
]
I would like to map the value of the array in the json to have :
[{key1 : "a", key2 : "b", key3: "c", index: 1},
{key1 : "d", key2 : "e", key3: "f", index: 2},
{key1 : "g", key2 : "h", key3: "i", index: 3}]
Is there an easy to do that ?
>Solution :
Assuming you meant "array of objects" and not "array of json", you can indeed do it with map this way:
const tab = [1,2,3]
const myArray = [
{key1: 'a', key2: 'b', key3:'c'},
{key1:'d', key2 : 'e', key3:'f'},
{key1 : 'g', key2 : 'h', key3:'i'}];
console.log(myArray.map((element, index) => { return {...element, index:tab[index]}; }));