Reputation: 95
How can I take two arrays such as these:
const age = [31,53,62]
const names = ['john', 'sam', 'kathy']
And format them into the following:
const data = {
"children": [
{ "id": 1,
"name": "john",
"age": 31,
},
{ "id": 2,
"name": "sam",
"age": 53,
},
{ "id": 3,
"name": "kathy",
"age": 62,
}
]
}
Upvotes: 0
Views: 50
Reputation: 22574
You can use array#map
to generate your array of object. You can map names with age using the index.
const age = [31,53,62],
names = ['john', 'sam', 'kathy'],
result = {children: age.map((a,i) => ({id: i+1, name: names[i], age: a}))};
console.log(result);
Upvotes: 2