我有兩組JSON值,裡面Year與Subject相同,最後Value值不同,但我想將兩組合併成一組(如圖)
//JSON 1
[{"Year":"2021","Subject":"SG","CountValue":"1498"},{"Year":"2021","Subject":"TW","CountValue":"8906"}]
//JSON 2
[{"Year":"2021","Subject":"SG","ApplicationValue":"1299"},{"Year":"2021","Subject":"TW","ApplicationValue":"7542"}]
//想合併成這樣
[{"Year":"2021","Subject":"SG","CountValue":"1498","ApplicationValue":"1299"},{"Year":"2021","Subject":"TW","CountValue":"8906","ApplicationValue":"7542"}]
我在查資料時,在這個網址有看到類似的資訊
https://stackoverflow.com/questions/47564159/merging-the-same-specific-json-object-in-two-different-json-arrays
本想著依樣畫葫蘆應該就可以解決,但最後顯示的確不是我想呈現的樣子,想請問該往哪個方向撰寫比較建議呢?
另外,嘗試解讀let result這段但還是不太清楚,想問一下這段大概在做甚麼呢?
//嘗試依樣畫葫蘆的語法
let allProducts = [
[{"Year":"2021","Subject":"SG","CountValue":"1498"},{"Year":"2021","Subject":"TW","CountValue":"8906"}]
];
let allLocations = [
[{"Year":"2021","Subject":"SG","ApplicationValue":"1299"},{"Year":"2021","Subject":"TW","ApplicationValue":"7542"}]
]
let result = allProducts.map((product) => {
let matchingLocation = allLocations.find((location) => {
return location.Subject == product.Subject;
});
return Object.assign(product, matchingLocation);
})
console.log(result);
//撈出的資料
[[{
ApplicationValue: "1299",
Subject: "SG",
Year: "2021"
}, {
ApplicationValue: "7542",
Subject: "TW",
Year: "2021"
}]]
更新:
若今天我有三個以上的組合要合併,應該怎麼撰寫比較好呢?
//嘗試依樣畫葫蘆的語法
let allProducts = [
[{"Year":"2021","Subject":"SG","CountValue":"1498"},{"Year":"2021","Subject":"TW","CountValue":"8906"}]
];
let allLocations = [
[{"Year":"2021","Subject":"SG","ApplicationValue":"1299"},{"Year":"2021","Subject":"TW","ApplicationValue":"7542"}]
]
//新增一組
let AA = [
[{"Year":"2021","Subject":"SG","AAValue":"222"},{"Year":"2021","Subject":"TW","AAValue":"333"}]
]
let result = allProducts.map((product) => {
let matchingLocation = allLocations.find((location) => {
return location.Subject == product.Subject;
});
return Object.assign(product, matchingLocation);
})
console.log(result);
感謝各位QQ
剛試看了一下,你的陣列有 2 層,改成 1 層似乎就可以了
// 二層陣列
let allProducts = [[]];
let allLocations = [[]];
// 修改成一層陣列
let allProducts = [];
let allLocations = [];
用 find 找到 Subject 相同值的陣列位置,然後將 2 者用 Object.assign 複製
相同的 key 會被後來的蓋過,新的會增加,所以得到新的物件
map 回傳這個新物件組合的陣列結果
(感覺很厲害的寫法!)
== 更新
多一組類似這樣?
let allProducts = [
{"Year":"2021","Subject":"SG","CountValue":"1498"},{"Year":"2021","Subject":"TW","CountValue":"8906"}
];
let allLocations = [
{"Year":"2021","Subject":"SG","ApplicationValue":"1299"},{"Year":"2021","Subject":"TW","ApplicationValue":"7542"}
];
//新增一組
let AA = [
{"Year":"2021","Subject":"SG","AAValue":"222"},{"Year":"2021","Subject":"TW","AAValue":"333"}
];
let result = allProducts.map((product) => {
let matchingLocation1 = allLocations.find((location) => {
return location.Subject == product.Subject;
});
let matchingLocation2 = AA.find((location) => {
return location.Subject == product.Subject;
});
return Object.assign(product, matchingLocation1, matchingLocation2);
})
console.log(result);
/*
[
{
"Year": "2021",
"Subject": "SG",
"CountValue": "1498",
"ApplicationValue": "1299",
"AAValue": "222"
},
{
"Year": "2021",
"Subject": "TW",
"CountValue": "8906",
"ApplicationValue": "7542",
"AAValue": "333"
}
]
*/
補充一下懶人的寫法,邏輯一樣,差在使用展開運算子展開物件
const result = allProducts.map((el) => ({
...el,
...allLocations.find((location) => location.Subject === el.Subject),
...AA.find((a) => a.Subject === el.Subject)
}))
listennn08
感覺很厲害,我再研究看看
自己做紀錄
其餘參數(rest parameter):https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Functions/rest_parameters
看起來是多了一個[],你試試看把allProducts跟allLocations的外層[]拿掉
let allProducts =
[{"Year":"2021","Subject":"SG","CountValue":"1498"},{"Year":"2021","Subject":"TW","CountValue":"8906"}];
let allLocations =
[{"Year":"2021","Subject":"SG","ApplicationValue":"1299"},{"Year":"2021","Subject":"TW","ApplicationValue":"7542"}] ;
這樣應該就可以了