Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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_Typescript - Fatal编程技术网

Javascript 将多个对象的特性映射到具有相同特性名称的单个对象的有效方法

Javascript 将多个对象的特性映射到具有相同特性名称的单个对象的有效方法,javascript,typescript,Javascript,Typescript,这就是我现在的做法。想知道是否有更好的方法,因为实际用例大约有150行。最好是有地图功能或者类似的东西 this.component={ 项目1:0, 项目2:2 } this.otherComponent={ 项目3:0, 项目4:2 } 此.form={ item1:this.component.item1, item2:this.component.item2, item3:this.otherComponent.item3, item4:this.otherComponent.item4

这就是我现在的做法。想知道是否有更好的方法,因为实际用例大约有150行。最好是有地图功能或者类似的东西

this.component={
项目1:0,
项目2:2
}
this.otherComponent={
项目3:0,
项目4:2
}
此.form={
item1:this.component.item1,
item2:this.component.item2,
item3:this.otherComponent.item3,
item4:this.otherComponent.item4,
}

您可能希望使用
对象。分配

this.form = Object.assign({}, this.component, this.otherComponent);
或对象扩展语法:

this.form = {...this.component, ...this.otherComponent};

您可能希望使用
对象。分配

this.form = Object.assign({}, this.component, this.otherComponent);
或对象扩展语法:

this.form = {...this.component, ...this.otherComponent};
当然可以

你可以使用

例如:

let组件={
项目1:0,
项目2:2
}
让其他组件={
项目3:0,
项目4:2
}
让form={…component,…otherComponent}
控制台。日志(表格)
当然可以

你可以使用

例如:

let组件={
项目1:0,
项目2:2
}
让其他组件={
项目3:0,
项目4:2
}
让form={…component,…otherComponent}

console.log(form)
嘿,你能做的就是把这些对象分散开来:

const a = {
  item1: 0,
  item2: 2
}

const b = {
  item3: 0,
  item4: 2
}
        
const result = {
 ...a,
 ...b
}



这将给出所需的结果

嘿,您可以做的是将这些对象展开:

const a = {
  item1: 0,
  item2: 2
}

const b = {
  item3: 0,
  item4: 2
}
        
const result = {
 ...a,
 ...b
}


这将给出期望的结果

“实际用例大约有150行”—还有更多的项目?有更多的组件吗?你是否真的有一个数组(或者可以很容易地把它们放在一个数组中)?“实际的用例大约有150行”——还有更多的项目?有更多的组件吗?你真的有一个数组(或者可以很容易地把它们放在一个数组中)?