![](/img/trans.png)
[英]Efficient way to get unique objects from array of objects, by object property?
[英]Efficient way to map properties from multiple objects to single object with same property names
這是我現在如何做的當前方式。 想知道是否有更好的方法,因為實際用例大約有 150 行。 最好使用 map function 或類似的東西。
this.component = {
item1: 0,
item2: 2
}
this.otherComponent = {
item3: 0,
item4: 2
}
this.form = {
item1: this.component.item1,
item2: this.component.item2,
item3: this.otherComponent.item3,
item4: this.otherComponent.item4,
}
您可能正在尋找使用Object.assign
:
this.form = Object.assign({}, this.component, this.otherComponent);
或 object 擴展語法:
this.form = {...this.component, ...this.otherComponent};
是的當然!
您可以使用(...) 傳播語法
this.component = {
item1: 0,
item2: 2
}
this.otherComponent = {
item3: 0,
item4: 2
}
this.form = { ...this.component, ...this.otherComponent }
例子:
let component = { item1: 0, item2: 2 } let otherComponent = { item3: 0, item4: 2 } let form = {...component, ...otherComponent} console.log(form)
嘿,您可以做的是將這些對象分散開來:
const a = {
item1: 0,
item2: 2
}
const b = {
item3: 0,
item4: 2
}
const result = {
...a,
...b
}
這將給出預期的結果
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.