關閉。這個問題需要除錯細節。它目前不接受答案。
編輯問題以包含所需的行為、特定問題或錯誤以及重現問題所需的最短代碼。這將幫助其他人回答問題。
3天前關閉。
改進這個問題根據物件鍵和陣列物件鍵映射將一個物件合并為陣列物件
{
'id':49,
'city':'bangalore',
'country':'India'
}
arr=[{
{title: 'Location ID', field: 'id', width: 200, _width: 200, filter: 'text'},
{title: 'City', field: 'city', width: 200, _width: 200, filter: 'dropdown'},
{title: 'Country', field: 'country', width: 200, _width: 200, filter: 'dropdown'}
}]
expected results:
[{
{'id':49,title: 'Location ID', field: 'id', width: 200, _width: 200, filter: 'text'},
{'city':'bangalore',title: 'City', field: 'city', width: 200, _width: 200, filter: 'dropdown'},
{'country':'India',title: 'Country', field: 'country', width: 200, _width: 200, filter: 'dropdown'}
}]```
I tried below code
console.log([...Obj,...arr])
uj5u.com熱心網友回復:
您可以將Array.prototype.map()與Destructuring assignment結合使用
代碼:
const obj = { id: 49, city: 'bangalore', country: 'India' }
const arr = [{title: 'Location ID',field: 'id',width: 200,_width: 200,filter: 'text',},{ title: 'City', field: 'city', width: 200, _width: 200, filter: 'dropdown' },{title: 'Country',field: 'country',width: 200,_width: 200,filter: 'dropdown',},]
const result = arr.map(({ field, ...o }) => ({
[field]: obj[field],
...o
}))
console.log(result)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/485973.html
標籤:javascript 数组 目的
上一篇:僅當鍵匹配時才合并2個物件陣列
