Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过动态键和值创建数组_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 通过动态键和值创建数组

Javascript 通过动态键和值创建数组,javascript,angular,typescript,Javascript,Angular,Typescript,我在变量的字符串中得到一个值,即 let name = 'Vishesh'; let name2 = 'Vishesh2'; 和一个数组,即 let cars = [{carName: "Mercedes"},{carName: "Volvo"},{carName:"BMW"}]; 现在我想创建一个数组,其中我的键是名称,值应该是cars数组,即 Array=[{Vishesh: [{carName: "Mercedes"},{carName: "Volvo"},{carName:"BMW"

我在变量的字符串中得到一个值,即

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"}]}];
这个怎么样(假设只有两个名字):

注意:两个对象引用相同的数组。因此,更改数组将在两个位置“更新”数组(因为它是通过引用进行的)

但是,如果您想要一组动态名称,您的代码可以如下所示:

var names = ["Vishesh", "Vishesh2", "Vishesh3"];

var array = [];
for(var i = 0; i < names.length; i++) {
    var name = names[i];

    var obj = {};
    obj[name] = cars;

    array.push(obj);
}
var name=[“Vishesh”、“Vishesh2”、“Vishesh3”];
var数组=[];
对于(var i=0;i
这个怎么样(假设只有两个名称):

注意:两个对象引用相同的数组。因此,更改数组将在两个位置“更新”数组(因为它是通过引用进行的)

但是,如果您想要一组动态名称,您的代码可以如下所示:

var names = ["Vishesh", "Vishesh2", "Vishesh3"];

var array = [];
for(var i = 0; i < names.length; i++) {
    var name = names[i];

    var obj = {};
    obj[name] = cars;

    array.push(obj);
}
var name=[“Vishesh”、“Vishesh2”、“Vishesh3”];
var数组=[];
对于(var i=0;i
您可以将名称作为键直接插入字典结构中

let name='Vishesh';
设name2='Vishesh2';
console.log([
{
[姓名]:[
{卡纳姆:“梅赛德斯”},
{卡纳姆:“沃尔沃”},
{卡纳姆:“宝马”}
]
},
{
[姓名2]:[
{卡纳姆:“梅赛德斯”},
{卡纳姆:“沃尔沃”},
{卡纳姆:“宝马”}
]
}

]);您可以将名称作为键直接插入字典结构中

let name='Vishesh';
设name2='Vishesh2';
console.log([
{
[姓名]:[
{卡纳姆:“梅赛德斯”},
{卡纳姆:“沃尔沃”},
{卡纳姆:“宝马”}
]
},
{
[姓名2]:[
{卡纳姆:“梅赛德斯”},
{卡纳姆:“沃尔沃”},
{卡纳姆:“宝马”}
]
}

]);如果始终有两个名称:

const Array = [
  { [name]: cars },
  { [name2]: cars }
]

如果始终有两个名称:

const Array = [
  { [name]: cars },
  { [name2]: cars }
]

在您的汽车阵列上使用reduce功能也是一种解决方案:

let name=[“Wishes1”、“Wishes2”、“Wishes3”]
let cars=[{carName:“梅赛德斯”},{carName:“沃尔沃”},{carName:“宝马”}];
常量withNames=(名称)=>(currentMapState、currentItem、currentIndex)=>{
currentMapState[names[currentIndex]]=currentItem;
返回当前映射状态;
}

log(cars.reduce(withNames(names),{}))在汽车阵列上使用reduce函数也可以是解决方案:

let name=[“Wishes1”、“Wishes2”、“Wishes3”]
let cars=[{carName:“梅赛德斯”},{carName:“沃尔沃”},{carName:“宝马”}];
常量withNames=(名称)=>(currentMapState、currentItem、currentIndex)=>{
currentMapState[names[currentIndex]]=currentItem;
返回当前映射状态;
}


log(cars.reduce(withNames(names),{}))您尝试过什么?始终只有两个名称?让myarray:any[]=[];设obj1:any={};obj1[名称]=车辆;myarray.push(obj1)No name1和name2只是一个示例,说明它将是多个。它们是动态值,因此不确定您将获得多少名称。@Eliseo请回答它,不要发表评论。您尝试过什么?始终只有2个名称?让myarray:any[]=[];设obj1:any={};obj1[名称]=车辆;myarray.push(obj1)No name1和name2只是一个示例,说明它将是多个。它们是动态值,所以不确定您将获得多少名称。@Eliseo请回答它,不要评论。酷,不知道您可以这样做!没有动态代码,它就像我想要的那样完美,但是动态代码并不合适,因为它不是数组@Mathyn dynamic的解决方案在他使用push时对我起了作用,所以我加入了数组。酷,我不知道你们能做到!没有动态代码,它就像我想要的那样完美,但是动态代码并不合适,因为它不是数组@Mathyn dynamic的解决方案在使用push时对我起了作用,所以我加入了数组。谢谢你的帮助,但我希望它加入数组。我对你的努力投了赞成票。哦,我明白了。对不起。我将继续使用reduce,而不是将其推到map,您可以这样将其推到数组:谢谢您的帮助,但我希望它在数组中。我对你的努力投了赞成票。哦,我明白了。对不起。我将继续使用reduce,而不是将其推送到map,您可以将其推送到数组,如下所示: