[英]JSON Array combine items based on an same ID
所以我有這些項目,它有一些重復的ID,但值不同。 它可能是更多的項目,但目前我使用這些項目。 數組結果如下:
"items": [
{
"id": "E46",
"size": "5 Ounce"
},
{
"id": "E46",
"color": "Green"
},
{
"id": "E32",
"size": "8 Ounce"
},
{
"id": "E32",
"color": "Green"
},
{
"id": "E4G",
"size": "5 Ounce"
},
{
"id": "E4G",
"color": "Pink"
},
{
"id": "E3C",
"size": "8 Ounce"
},
{
"id": "E3C",
"color": "Pink"
}
]
我想結合這些並使它看起來像這樣:
"items": [
{
"id": "E46",
"size": "5 Ounce"
"color": "Green"
},
{
"id": "E32",
"size": "8 Ounce"
"color": "Green"
},
{
"id": "E4G",
"size": "5 Ounce"
"color": "Pink"
},
{
"id": "E3C",
"size": "8 Ounce"
"color": "Pink"
}
]
如果有可能,謝謝:
你可以這樣做:
var data = { items:
[{ id: 'E46', size: '5 Ounce' },
{ id: 'E46', color: 'Green' },
{ id: 'E32', size: '8 Ounce' },
{ id: 'E32', color: 'Green' },
{ id: 'E4G', size: '5 Ounce' },
{ id: 'E4G', color: 'Pink' },
{ id: 'E3C', size: '8 Ounce' },
{ id: 'E3C', color: 'Pink' } ] }
var result = {}
data.items.forEach(function (item) { //1
Object.keys(item).forEach(function(key) { //2
if (!result[item.id]) result[item.id] = {}; //3
result[item.id][key] = item[key]; //4
});
})
以下是發生的事情:
// 1我們循環遍歷items
數組中的每個項目
// 2我們遍歷items
數組中單個item
每個key
// 3我們使用item
id和一個空對象創建一個鍵值配對
// 4我們在 result
對象中添加新item
對象的當前鍵值對。
然后, result
對象將具有此值:
{ E46: { id: 'E46', size: '5 Ounce', color: 'Green' },
E32: { id: 'E32', size: '8 Ounce', color: 'Green' },
E4G: { id: 'E4G', size: '5 Ounce', color: 'Pink' },
E3C: { id: 'E3C', size: '8 Ounce', color: 'Pink' } }
如果你想把它放回一個數組中,你所要做的就是遍歷對象中的每個鍵值對並將所有值添加到數組中,如下所示:
var arrayResult = []
Object.keys(result).forEach(function(key) {
arrayResult.push(result[key])
});
然后你的arrayResult
變量將具有此值:
[ { id: 'E46', size: '5 Ounce', color: 'Green' },
{ id: 'E32', size: '8 Ounce', color: 'Green' },
{ id: 'E4G', size: '5 Ounce', color: 'Pink' },
{ id: 'E3C', size: '8 Ounce', color: 'Pink' } ]
你可以在你的程序中的任何其他地方使用它。
function mergeObjectWithSameId(items) {
let output = []
items.forEach((item) => {
if (output.filter(existingItem => existingItem.id === item.id).length === 0) {
output.push(item)
}
else {
const existingItem = output.filter(existingItem => existingItem.id === item.id)[0]
Object.keys(item).forEach(key => {
if(!existingItem[key]) {
existingItem[key] = item[key]
}
})
}
})
return output
}
我在Chrome控制台中對其進行了測試。
只需運行mergeObjectWithSameId(items)
和Voila即可獲得期待的結果!
假設您正在尋找JavaScript解決方案,並且您已經將問題中的' itemsParsed
解析為JavaScript對象(下面的itemsParsed
),您可以itemsParsed
實現數據重組。
第一個是創建數據的hashmap,其中重復的'id'屬性表示新Object中的鍵,如此...
var itemsParsed = theParsedJSON;
var itemHolder = (function() {
var O = {}, ID;
itemsParsed.forEach(function(o) {
Object.keys(o).forEach(function(a) {
if (a === 'id') {
ID = o.id;
if (!O[ID]) O[ID] = {};
} else {
O[ID][a] = o[a];
}
});
});
return O;
}());
這將返回Object itemHolder
,因為......
itemHolder = {
E46: {
size: "5 Ounce"
color: "Green"
}
E32: {
size: "8 Ounce"
color: "Green"
}
E4G: {
size: "5 Ounce"
color: "Pink"
}
E3C: {
size: "8 Ounce"
color: "Pink"
}
};
下一步是在另一個步驟轉換中使用這個結構,從中可以創建新的對象並將其推送到新的數組中,就像這樣......
var newItems = [];
Object.keys(itemHolder).forEach(function(n) {
var tmp = itemHolder[n];
var newObj = {
id: n
};
Object.keys(tmp).forEach(function(x) {
newObj[x] = tmp[x];
});
newItems.push(newObj);
});
當JSON.stringify方法應用於它時,可以看到newItems
數組的結構......
console.log(JSON.stringify(newItems, null, 4));
/* ==>
[
{
"id": "E46",
"size": "5 Ounce",
"color": "Green"
},
{
"id": "E32",
"size": "8 Ounce",
"color": "Green"
},
{
"id": "E4G",
"size": "5 Ounce",
"color": "Pink"
},
{
"id": "E3C",
"size": "8 Ounce",
"color": "Pink"
}
]
*/
希望有所幫助。 :)
MDN: Object.keys()
- > https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
MDN: Array.forEach()
- > https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
你可以reduce
數組。 使用每個唯一id
作為鍵創建一個累加器對象,並根據id
使用Object.assign()
合並每個對象
const items=[{id:"E46",size:"5 Ounce"},{id:"E46",color:"Green"},{id:"E32",size:"8 Ounce"},{id:"E32",color:"Green"},{id:"E4G",size:"5 Ounce"},{id:"E4G",color:"Pink"},{id:"E3C",size:"8 Ounce"},{id:"E3C",color:"Pink"}]; const merged = items.reduce((acc, o) => { acc[o.id] = Object.assign(acc[o.id] || {}, o) return acc; }, {}) const output = Object.values(merged) console.log(output)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.