How to create an array of objects that has two properties name
and shade
?
[{
"name": "black",
"shade": "dark"
}, {
"name": "white",
"shade": "light"
},
{
"name": "red",
"shade": "dark"
}, {
"name": "blue",
"shade": "dark"
},
{
"name": "yellow",
"shade": "light"
}
]
I have two different arrays now.
name = ["black","white","red","blue","yellow"]
shade = ["dark","light","dark","dark","light"]
How can I achieve this?
Use map
var output = name.map( (s, i) => ({name : s, shade : shade[i]}) );
Demo
var name1 = ["black","white","red","blue","yellow"];
var shade = ["dark","light","dark","dark","light"];
var output = name1.map( (s, i) => ({name : s, shade : shade[i]}) );
console.log( output );
var new_array = [];
// assuming the arrays have the same length
for (var i = 0; i < name.length; i++)
new_array.push({name: name[i], shade: shade[i]});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With