I have the below array
let array= ['shorts', 'tees']
from this I want an array that looks like this
let newArray= [{shorts: []},{tees: []}, ]
can anyone help?
I have the below array
let array= ['shorts', 'tees']
from this I want an array that looks like this
let newArray= [{shorts: []},{tees: []}, ]
can anyone help?
Just loop over the array with map()
and convert each element to the object you want. You can use the syntax [someString]
as a property name in an object literal to create that property.
let array = ['shorts', 'tees'];
let newArray = array.map(a => ({ [a]: [] }));
console.log(newArray);
let array= ['shorts', 'tees'];
let result = array.map(elem => ({[elem]: []}));
console.log(result);
let array= ['shorts', 'tees']
let list = [];
for(let item of array) {
let obj = {};
obj[item] = [];
list.push(obj)
}
console.log(list)