我在变量中得到一个字符串值,即
let name = 'Vishesh';
let name2 = 'Vishesh2';
以及一个数组,即
let cars = [{carName: "Mercedes"},{carName: "Volvo"},{carName:"BMW"}];
现在我想创建一个数组,其中我的键是名称和值,应该有cars数组,即
Array=[{Vishesh: [{carName: "Mercedes"},{carName: "Volvo"},{carName:"BMW"}]},{Vishesh2: [{carName: "Mercedes"},{carName: "Volvo"},{carName:"BMW"}]}];
最佳答案
let name = 'Vishesh';
let name2 = 'Vishesh2';
let cars = [{carName: "Mercedes"},{carName: "Volvo"},{carName:"BMW"}];
let array = []
array.push({[name]:cars})
array.push({[name2]:cars})
console.log(array);