合并对象和数组映射对象键

merge Object and array mapping object key

根据对象键和映射的数组对象键将一个对象合并到数组对象中

{
'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])

您可以使用Array.prototype.map() combined with 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)