[英]javascript object array sum values based on two criteria
Hi I have just started in javascript. 嗨,我刚刚开始使用javascript。 I have been trying to sum my object array by ZOPR_TNLO and add it to a new array. 我一直在尝试通过ZOPR_TNLO对我的对象数组求和并将其添加到新数组中。
so i get a new array with ZOPR_TNLO:, Actions, Values. 所以我得到一个带有ZOPR_TNLO :, Actions,Values的新数组。 Please help :) 请帮忙 :)
var acountsJson = [{A0CALMONTH_T: "JAN 2015", ZOPR_TNLO: "OP.BUBBLES", Actions: "Arrests", Values: "1"},
{A0CALMONTH_T: "JAN 2015", ZOPR_TNLO: "OP.BUBBLES", Actions: "DrinkDriving", Values: "2"},
{A0CALMONTH_T: "JAN 2015", ZOPR_TNLO: "OP.BUBBLES", Actions: "DrinkDriving", Values: "2"},
{A0CALMONTH_T: "DEC 2017", ZOPR_TNLO: "DECEMBER 2017", Actions: "Arrests", Values: "3"},
{A0CALMONTH_T: "DEC 2017", ZOPR_TNLO: "DECEMBER 2017", Actions: "DrinkDriving", Values: "0"},
{A0CALMONTH_T: "DEC 2017", ZOPR_TNLO: "DECEMBER 2017", Actions: "Arrests", Values: "5"},
{A0CALMONTH_T: "DEC 2017", ZOPR_TNLO: "DECEMBER 2017", Actions: "DrinkDriving", Values: "0"}
];
var task = ["DECEMBER 2017", "OP.BUBBLES"];
var kpi = ["Arrests", "DrinkDriving"]
var resultsArray = [];
var resultsArray1 = [];
var summedValues1 = 0;
for (var p = 0; p < task.length; p++) { //takes the first task from list of task
for (var e = 0; e < kpi.length; e++) { //takes the kpi from the list
for (var i = 0; i < acountsJson.length; i++) {
if(acountsJson[i].ZOPR_TNLO === task[p] && acountsJson[i].Actions === kpi[e]){
//console.log(e);
summedValues1 += Number(acountsJson[i].Values);
var task1 = acountsJson[i].ZOPR_TNLO;
var Actions1 = acountsJson[i].Actions;
}
}//thrid loop which loops through the json data
var index = acountsJson.findIndex(x => x.ZOPR_TNLO==task1 &&
x.Actions==Actions1)
// here you can check specific property for an object whether it exist in
your array or not
/// Trying to add this to check before push
if (index === -1){
resultsArray1.push({
ZOPR_TNLO: task1,
Actions: Actions1,
SUMMED_VALUES: summedValues1
})
console.log("no");
}
else
console.log("object already exists");
summedValues1 = 0;
} //second for loop
}//first loop
https://playcode.io/132805?tabs=console&script.js&output https://playcode.io/132805?tabs=console&script.js&output
Getting the error "uncaught syntax error unexpected token" for my indexof statement. 为我的indexof语句获取错误“未捕获的语法错误意外令牌”。
I could be going about it completely wrong. 我可能会完全错了。 All i am trying to do is sum my acountsJson by two keys ZOPR_TNLO and Actions (summed value in Values). 我要做的就是用两个键ZOPR_TNLO 和 Actions(“值”中的求和值)对acountsJson求和。
Create a new object array that has unique values by the keys eg 通过按键创建一个具有唯一值的新对象数组,例如
ZOPR_TNLO Actions Values(summed)
OP.BUBBLES DrinkDriving 4
OP.BUBBLES Arrests 1
December 2017 Arrests 8
December 2017 DrinkDriving 0
You could take Map
with later Array.from
for collecting the wanted data and generating the wanted result set. 您可以将Map
与更高版本的Array.from
以收集所需的数据并生成所需的结果集。
var data = [{ A0CALMONTH_T: "JAN 2015", ZOPR_TNLO: "OP.BUBBLES", Actions: "Arrests", Values: "1" }, { A0CALMONTH_T: "JAN 2015", ZOPR_TNLO: "OP.BUBBLES", Actions: "DrinkDriving", Values: "2" }, { A0CALMONTH_T: "JAN 2015", ZOPR_TNLO: "OP.BUBBLES", Actions: "DrinkDriving", Values: "2" }, { A0CALMONTH_T: "DEC 2017", ZOPR_TNLO: "DECEMBER 2017", Actions: "Arrests", Values: "3" }, { A0CALMONTH_T: "DEC 2017", ZOPR_TNLO: "DECEMBER 2017", Actions: "DrinkDriving", Values: "0" }, { A0CALMONTH_T: "DEC 2017", ZOPR_TNLO: "DECEMBER 2017", Actions: "Arrests", Values: "5" }, { A0CALMONTH_T: "DEC 2017", ZOPR_TNLO: "DECEMBER 2017", Actions: "DrinkDriving", Values: "0" }], task = ["DECEMBER 2017", "OP.BUBBLES"], // ZOPR_TNLO kpi = ["Arrests", "DrinkDriving"], // Actions result = Array.from( data.reduce((m, { ZOPR_TNLO, Actions, Values }) => (key => task.includes(ZOPR_TNLO) && kpi.includes(Actions) ? m.set(key, (m.get(key) || 0) + +Values) : m)([ZOPR_TNLO, Actions].join('|')), new Map() ), ([k, Values]) => (([ZOPR_TNLO, Actions]) => ({ ZOPR_TNLO, Actions, Values }))(k.split('|')) ); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.