基于两个条件的javascript对象数组总和值

时间:2018-10-12 11:04:55

标签: javascript arrays object criteria

嗨,我刚刚开始使用javascript。我一直在尝试通过ZOPR_TNLO对我的对象数组求和并将其添加到新数组中。

所以我得到一个带有ZOPR_TNLO :, Actions,Values的新数组。请帮忙:)

 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

为我的indexof语句获取错误“未捕获的语法错误意外令牌”。

我可能完全错了。我要做的就是通过两个键ZOPR_TNLO Actions(“值”中的求和值)来求和。

使用例如键创建一个具有唯一值的新对象数组。

    ZOPR_TNLO       Actions     Values(summed)
  OP.BUBBLES        DrinkDriving        4
  OP.BUBBLES        Arrests            1
  December 2017     Arrests            8
  December 2017     DrinkDriving        0

1 个答案:

答案 0 :(得分:0)

您可以将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; }