我已經在Stack上搜索了此問題,但似乎無法找到答案。如果使用reduce,for each或filter共享屬性值,則將JSON數組中的對象結合起來
我工作的一個項目,並試圖找出如何減少對象的數量在一個JSON數組,並從共享使用.reduce
,.forEach
,或.filter
的name
屬性的值對象結合value
S,以便具有給定名稱的所有值將被配對在一起。我可以用reduce
和一個附加的鍵控對象來做到這一點,但我想把它作爲一個簡單的JSON數組。我已經嘗試了各種排列filter
和reduce
,但他們似乎正在返回奇怪的錯誤。
這裏的JavaScript代碼,我的工作:
//Json Data
var json = [{
"name": "Value0",
"value": "Sample0",
"truthy": true
}, {
"name": "Value0",
"value": "Sample1",
"truthy": false
}, {
"name": "Value0",
"value": "Sample2",
"truthy": true
}, {
"name": "Value0",
"value": "Sample3",
"truthy": false
}, {
"name": "Value1",
"value": "Sample4",
"truthy": true
}, {
"name": "Value1",
"value": "Sample5",
"truthy": false
}, {
"name": "Value1",
"value": "Sample6",
"truthy": true
}, {
"name": "Value1",
"value": "Sample7",
"truthy": false
}, {
"name": "Value2",
"value": "Sample8",
"truthy": true
}, {
"name": "Value2",
"value": "Sample9",
"truthy": false
}, {
"name": "Value2",
"value": "Sample10",
"truthy": true
}, {
"name": "Value2",
"value": "Sample11",
"truthy": false
}, {
"name": "Value3",
"value": "Sample12",
"truthy": true
}, {
"name": "Value3",
"value": "Sample13",
"truthy": false
}, {
"name": "Value3",
"value": "Sample14",
"truthy": true
}, {
"name": "Value3",
"value": "Sample15",
"truthy": false
}, {
"name": "Value4",
"value": "Sample16",
"truthy": true
}, {
"name": "Value4",
"value": "Sample17",
"truthy": false
}, {
"name": "Value4",
"value": "Sample18",
"truthy": true
}, {
"name": "Value4",
"value": "Sample19",
"truthy": false
}, {
"name": "Value5",
"value": "Sample20",
"truthy": true
}, {
"name": "Value5",
"value": "Sample21",
"truthy": false
}, {
"name": "Value5",
"value": "Sample22",
"truthy": true
}, {
"name": "Value5",
"value": "Sample23",
"truthy": false
}, {
"name": "Value6",
"value": "Sample24",
"truthy": true
}, {
"name": "Value6",
"value": "Sample25",
"truthy": false
}, {
"name": "Value6",
"value": "Sample26",
"truthy": true
}, {
"name": "Value6",
"value": "Sample27",
"truthy": false
}, {
"name": "Value7",
"value": "Sample28",
"truthy": true
}, {
"name": "Value7",
"value": "Sample29",
"truthy": false
}, {
"name": "Value7",
"value": "Sample30",
"truthy": true
}, {
"name": "Value7",
"value": "Sample31",
"truthy": false
}, {
"name": "Value8",
"value": "Sample32",
"truthy": true
}, {
"name": "Value8",
"value": "Sample33",
"truthy": false
}, {
"name": "Value8",
"value": "Sample34",
"truthy": true
}, {
"name": "Value8",
"value": "Sample35",
"truthy": false
}, {
"name": "Value9",
"value": "Sample36",
"truthy": true
}, {
"name": "Value9",
"value": "Sample37",
"truthy": false
}, {
"name": "Value9",
"value": "Sample38",
"truthy": true
}, {
"name": "Value9",
"value": "Sample39",
"truthy": false
}, {
"name": "Value10",
"value": "Sample40",
"truthy": true
}, {
"name": "Value10",
"value": "Sample41",
"truthy": false
}, {
"name": "Value10",
"value": "Sample42",
"truthy": true
}, {
"name": "Value10",
"value": "Sample43",
"truthy": false
}, {
"name": "Value11",
"value": "Sample44",
"truthy": true
}, {
"name": "Value11",
"value": "Sample45",
"truthy": false
}, {
"name": "Value11",
"value": "Sample46",
"truthy": true
}, {
"name": "Value11",
"value": "Sample47",
"truthy": false
}, {
"name": "Value12",
"value": "Sample48",
"truthy": true
}, {
"name": "Value12",
"value": "Sample49",
"truthy": false
}, {
"name": "Value12",
"value": "Sample50",
"truthy": true
}, {
"name": "Value12",
"value": "Sample51",
"truthy": false
}, {
"name": "Value13",
"value": "Sample52",
"truthy": true
}, {
"name": "Value13",
"value": "Sample53",
"truthy": false
}, {
"name": "Value13",
"value": "Sample54",
"truthy": true
}, {
"name": "Value13",
"value": "Sample55",
"truthy": false
}, {
"name": "Value14",
"value": "Sample56",
"truthy": true
}, {
"name": "Value14",
"value": "Sample57",
"truthy": false
}, {
"name": "Value14",
"value": "Sample58",
"truthy": true
}, {
"name": "Value14",
"value": "Sample59",
"truthy": false
}, {
"name": "Value15",
"value": "Sample60",
"truthy": true
}, {
"name": "Value15",
"value": "Sample61",
"truthy": false
}, {
"name": "Value15",
"value": "Sample62",
"truthy": true
}, {
"name": "Value15",
"value": "Sample63",
"truthy": false
}, {
"name": "Value16",
"value": "Sample64",
"truthy": true
}, {
"name": "Value16",
"value": "Sample65",
"truthy": false
}, {
"name": "Value16",
"value": "Sample66",
"truthy": true
}, {
"name": "Value16",
"value": "Sample67",
"truthy": false
}, {
"name": "Value17",
"value": "Sample68",
"truthy": true
}, {
"name": "Value17",
"value": "Sample69",
"truthy": false
}, {
"name": "Value17",
"value": "Sample70",
"truthy": true
}, {
"name": "Value17",
"value": "Sample71",
"truthy": false
}, {
"name": "Value18",
"value": "Sample72",
"truthy": true
}, {
"name": "Value18",
"value": "Sample73",
"truthy": false
}, {
"name": "Value18",
"value": "Sample74",
"truthy": true
}, {
"name": "Value18",
"value": "Sample75",
"truthy": false
}, {
"name": "Value19",
"value": "Sample76",
"truthy": true
}, {
"name": "Value19",
"value": "Sample77",
"truthy": false
}, {
"name": "Value19",
"value": "Sample78",
"truthy": true
}, {
"name": "Value19",
"value": "Sample79",
"truthy": false
}, {
"name": "Value20",
"value": "Sample80",
"truthy": true
}, {
"name": "Value20",
"value": "Sample81",
"truthy": false
}, {
"name": "Value20",
"value": "Sample82",
"truthy": true
}, {
"name": "Value20",
"value": "Sample83",
"truthy": false
}, {
"name": "Value21",
"value": "Sample84",
"truthy": true
}, {
"name": "Value21",
"value": "Sample85",
"truthy": false
}, {
"name": "Value21",
"value": "Sample86",
"truthy": true
}, {
"name": "Value21",
"value": "Sample87",
"truthy": false
}, {
"name": "Value22",
"value": "Sample88",
"truthy": true
}, {
"name": "Value22",
"value": "Sample89",
"truthy": false
}, {
"name": "Value22",
"value": "Sample90",
"truthy": true
}, {
"name": "Value22",
"value": "Sample91",
"truthy": false
}, {
"name": "Value23",
"value": "Sample92",
"truthy": true
}, {
"name": "Value23",
"value": "Sample93",
"truthy": false
}, {
"name": "Value23",
"value": "Sample94",
"truthy": true
}, {
"name": "Value23",
"value": "Sample95",
"truthy": false
}, {
"name": "Value24",
"value": "Sample96",
"truthy": true
}, {
"name": "Value24",
"value": "Sample97",
"truthy": false
}, {
"name": "Value24",
"value": "Sample98",
"truthy": true
}, {
"name": "Value24",
"value": "Sample99",
"truthy": false
}];
//removing irrelevant data items
json = json.map(function(item) {
return {
"name": item.name,
"values": [item.value]
};
});
//attempt at reduce function
json.reduce(function(p, c) {
if (p && p.name === c.name) {
p.value.push(c.value);
}
});
/*
//attempt at filter function
json = json.filter(function(item,i) {
if (i > 0 && item.name === json[i-1].name) {
json[i-1].value.push(item.name);
}
});
//forEach attempt /*
json = json.forEach(function(item, i, arr) {
if (i > 0) {
if (arr[i].name === arr[i - 1].name) {
var pop = json.pop();
json[i - 1].value.push(pop.value);
}
}
});
*/
console.log(JSON.stringify(json));
理想情況下,我想有這樣的:
var json = [{
"name": "Value0",
"values": ["Sample0", "Sample1", "Sample2", "Sample3"]
}, {
"name": "Value1",
"values": ["Sample4", "Sample5", "Sample6", "Sample7"]
}
等
什麼是應該發生的'truthy'財產?你的輸出在第二個對象中,但它爲什麼是'真'? – nnnnnn
它應該消失。 – litel
如果它應該消失,您應該修改您的「理想」部分,因爲您顯示它仍然存在於其中一個對象中。另外,你是否可以假設所有具有相同名稱的對象將被組合在一起?即所有的「Value0」對象會先出現,然後是所有的「Value1」對象,然後是所有的「Value2」對象?或者他們可以混合起來? – Elezar