[英]How to group an array object based on three keys
基於這個問題Group array items using object
我引用了這個答案,由https://stackoverflow.com/users/1094311/1983回答
所以我從這個 jquery 獲取數據然后我解析它,之后我將它推送到變量
我從 myurl 得到的數據是這樣的
{"pname":"some1","datax":2.0643278,"prov":"11","datay":1},{"pname":"some1","datax":3.2142857142857144,"prov":"11","datay":1},{"pname":"some2","datax":1.125,"prov":"12","datay":1},{"pname":"some2","datax":1.6666666666666667,"prov":"12","datay":2}
這就是我的陣列開始的方式
$(document).ready(function(){
$.ajax({
url: "myurl",
method: "GET",
success: function(data) {
var parsedJSON = JSON.parse(data);
var result1 = [
(function(data) {
let tmp = [];
data.forEach(e => {
tmp.push({
type: "firstid",
prov: e.prov,
name: e.pname,
showInLegend:false,
dataPoints:[{x:e.datax, y:e.datay}],
})
});
return tmp;
})(parsedJSON)
];
從那里我得到了 result1[0],如果我控制台記錄它,它會是這樣的
[ { type: "firstid",
prov: 11,
name: some1,
showInLegend:false,
dataPoints:[{x:2.0643278, y:1}]
},
{ type: "firstid",
prov: 11,
name: some1,
showInLegend:false,
dataPoints:[{x:3.2142857142857144, y:1}]
},
{ type: "firstid",
prov: 12,
name: some2,
showInLegend:false,
dataPoints:[{x:1.125, y:1}]
},
{ type: "firstid",
prov: 12,
name: some2,
showInLegend:false,
dataPoints:[{x:1.6666666666666667, y:1}]
}]
當我控制台記錄它時,我想得到一些數組是這樣的:
[ { type: "firstid",
prov: 11,
name: some1,
showInLegend:false,
dataPoints:[ {x:2.0643278, y:1},
{x:3.2142857142857144, y:1}]
},
{ type: "firstid",
prov: 12,
name: some2,
showInLegend:false,
dataPoints:[ {x:1.125, y:1},
{x:1.6666666666666667, y:1}]
}]
我花了我的時間試圖通過數組合並來解決它,但似乎我無法弄清楚,任何幫助都會很好
根據您的方法,如果元素不存在,我將嘗試僅推送,如果存在,則推送到現有元素上的 dataPoints 數組。 我沒有試過這個,但它可以工作。
data.forEach(e => {
const tempIndex = tmp.findIndex(el => el.prov === e.prov);
if (tempIndex) {
tmp[tempIndex].dataPoints.push({ x: e.datax, y: e.datay });
} else {
tmp.push({
type: "firstid",
prov: e.prov,
name: e.pname,
showInLegend: false,
dataPoints: [{ x: e.datax, y: e.datay }],
})
}
});
請讓我知道這種編輯方法是否更好
使用Array.prototype.reduce
和Array.prototype.find
應該可以解決問題。
reduce
循環遍歷數組中的每個元素。 它以一個空數組開始: []
。 然后代碼使用find
檢查元素是否已經在新數組中。 如果是這樣,將dataPoints
添加到現有數組中。 如果它不存在,則將對象推送到新數組。
const obj = [{"pname":"some1","datax":2.0643278,"prov":"11","datay":1},{"pname":"some1","datax":3.2142857142857144,"prov":"11","datay":1},{"pname":"some2","datax":1.125,"prov":"12","datay":1},{"pname":"some2","datax":1.6666666666666667,"prov":"12","datay":2}]; objReduced = obj.reduce((acc, cur) => { const dbItem = acc.find( element => element.prov === cur.prov); if (dbItem) { dbItem.dataPoints.push({x: cur.datax, y: cur.datay}); } else { acc.push({ type: "firstid", prov: cur.prov, name: cur.pname, showInLegend: false, dataPoints: [{x: cur.datax, y: cur.datay}] }); } return acc; }, []); console.log(objReduced);
obj = [{ type: "firstid", prov: 11, name: "some1", showInLegend: false, dataPoints: [{ x: 2.0643278, y: 1 }]
在合並它們之前,這還將檢查 dataPoints 數組中的重復項:
const data = [{ type: "firstid", prov: 11, name: "some1", showInLegend: false, dataPoints: [{ x: 2.0643278, y: 1 }] }, { type: "firstid", prov: 11, name: "some1", showInLegend: false, dataPoints: [{ x: 3.2142857142857144, y: 1 }] }, { type: "firstid", prov: 12, name: "some2", showInLegend: false, dataPoints: [{ x: 1.125, y: 1 }] }, { type: "firstid", prov: 12, name: "some2", showInLegend: false, dataPoints: [{ x: 1.6666666666666667, y: 1 }] } ]; const res = Object.values(data.reduce((acc, curr) => { const groupKey = ['type', 'prov', 'name', 'showInLegend'].map(x => curr[x]).join('-'); if (!acc[groupKey]) { acc[groupKey] = curr; } else { for (const { x: currX, y: currY } of curr.dataPoints) { for (const { x: accX, y: accY } of acc[groupKey].dataPoints) { if ((currX + '-' + currY) !== (accX + '-' + accY)) { acc[groupKey].dataPoints.push({ x: currX, y: currY }); } } } } return acc; }, {})); console.log(res);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.