串列動物.json
[
0:
{
base: {address: 'address house 1', animal: 'DOG', name: 'boby'}
},
1:
{
base: {address: 'address house 2', animal: 'CAT', name: 'pippo'}
},
2:
{base: {address: 'address house 3', animal: 'TIGER', name: 'best'}
},
3:
{base: {address: 'address house 4', animal: 'HORSE', name: 'lino'}
},
]
其他資訊.json
[{
"boby":
{ "id":1,
"img":"link img dog",
"forum":" link forum dog"
},
"pippo":
{ "id":2,
"img":"link img cat",
"forum":" link forum cat"
},
"best":
{ "id":3,
"img":" link img tiger",
"forum":" link forum tiger"
}, ]
大家早上好,我有這兩個無法連接的 json 檔案,我想讓我解釋一下,最終的結果應該是,你必須加入具有相同名稱和索引的人
[
0:
{
base: {address: 'address house 1', animal: 'DOG', name: 'boby'}
"id":1,
"img":"link img dog",
"forum":" link forum dog"
},
etc
我嘗試了幾種方法“Object.assign()”,“Object.concat()我將物件添加到陣列中,但作為新索引
uj5u.com熱心網友回復:
你可以這樣做
const info = [
{
"boby":
{
"id": 1,
"img": "link img dog",
"forum": " link forum dog"
}
},
{
"pippo":
{
"id": 2,
"img": "link img cat",
"forum": " link forum cat"
}
},
{
"best":
{
"id": 3,
"img": " link img tiger",
"forum": " link forum tiger"
}
},
]
const infoObj = Object.assign({}, ...info)
const animals = [
{
base: {
address: 'address house 1',
animal: 'DOG',
name: 'boby'
}
},
{
base: {
address: 'address house 2',
animal: 'CAT',
name: 'pippo'
}
},
{
base: {
address: 'address house 3',
animal: 'TIGER',
name: 'best'
}
},
{
base: {
address: 'address house 4',
animal: 'HORSE',
name: 'lino'
}
},
]
const result = animals.map(a => ({
...a,
...(infoObj[a.base.name] || {})
}))
console.log(result)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/474752.html
標籤:javascript
