I have two arrays and I want to store them in one array and create them as an object.
const name = ["Amy", "Robert", "Sofie"];
const age = ["21", "28", "25"];
The output I want is:
const person =[{name: 'Amy', age: '21'}, {name: 'Robert', age: '28'}, {name: 'Sofie', age: '25'}];
Is there a way that I can loop it to make it like this because my array is very long would be bothersome to manually type it. Thanks.
2
Answers
Given that the lengths of the two arrays are the same, you can use
map
function to achieve this.You could use Array.map like:
Take in consideration that the arrays length have to be equal.
And use plural when naming Arrays.