简体   繁体   English

遍历包含对象的数组的数组,并在JavaScript / Angular中更新数组

[英]Loop through an array of arrays containing object, and update the array in JavaScript/Angular

I am trying to loop through an array of arrays with object [[{},{},{}],[{},{},{}]] , and create a new array that basically "totals" the arrays. 我正在尝试遍历对象为[[{},{},{}],[{},{},{}]]数组数组,并创建一个新的数组,该数组基本上“总计”了这些数组。 I am a bit at a loss on how to achieve this. 我对如何实现这一点感到茫然。

My data looks like this: 我的数据如下所示:

[
  [{
    "2017-01-05": 607
  }, {
    "2017-01-06": 430
  }, {
    "2017-01-07": 357
  }, {
    "2017-01-08": 277
  }],
  [{
    "2017-01-09": 607
  }, {
    "2017-01-10": 430
  }, {
    "2017-01-11": 357
  }, {
    "2017-01-12": 277
  }]
],

I would like to "count" and "label" each week, and total each week. 我想每周“计数”和“标签”,每周总计。 Example: 例:

newArray: [{"Week 1": 1075}, {"Week 2": 1590}]

I know I should probably use a forEach loop, but then it gets a bit sketchy: 我知道我可能应该使用forEach循环,但是这样会有点粗略:

dateArray.forEach( function (arrayWeek)
{
  // push and name etc. functionality
});

I would greatly appreciate your assistance and guidance. 非常感谢您的协助和指导。

I would use the map function and reduce each week inside the map: 我将使用map函数并减少map内的每个星期:

 var days = [ [{"2017-01-05":607}, {"2017-01-06":430}, {"2017-01-07":357}, {"2017-01-08":277}], [{"2017-01-09":607}, {"2017-01-10":430}, {"2017-01-11":357}, {"2017-01-12":277}] ]; function aggregator(memo, day) { for (var i in day) { memo += day[i]; } return memo; } // Original version from the question var weeks = days.map(function(week, index) { var obj = {}; obj['Week ' + (index + 1)] = week.reduce(aggregator, 0); return obj; }); console.log(weeks); // Follow up version from question in the comments var weeks2 = days.map(function(week, index) { return { name: 'week ' + (index + 1), total: week.reduce(aggregator, 0) }; }); console.log(weeks2); 

You can try something like this. 您可以尝试这样。

 var data=[[{"2017-01-05":607},{"2017-01-06":430},{"2017-01-07":357},{"2017-01-08":277}],[{"2017-01-09":407},{"2017-01-10":430},{"2017-01-11":357},{"2017-01-12":277}]]; var result = data.reduce(function(p, c, i) { var total = c.reduce(function(total, obj) { for (var k in obj) { total += obj[k]; } return total; }, 0); // Format object in any format you want var tmp = {}; tmp.name = "Week " + (i+1) tmp.total = total; // Push formatted object in array p.push(tmp) return p; }, []) console.log(result) 

Note, I'd suggest you to use an object instead of array of objects. 注意,我建议您使用一个对象而不是对象数组。 Benefit of this would be that you will not require to loop over output to get value. 这样做的好处是您将不需要循环输出即可获得价值。 You can directly so result['week'+index] 您可以直接获得result['week'+index]

 var data=[[{"2017-01-05":607},{"2017-01-06":430},{"2017-01-07":357},{"2017-01-08":277}],[{"2017-01-09":407},{"2017-01-10":430},{"2017-01-11":357},{"2017-01-12":277}]]; var result = data.reduce(function(p, c, i) { var total = c.reduce(function(total, obj) { for (var k in obj) { total += obj[k]; } return total; }, 0); p["week" + (i + 1)] = total; return p; }, {}) console.log(result) 

the variable weeks should hold what you want... I'm assuming the week number is the actual week number in the year and not some index in the array. 可变的周应该拥有您想要的...我假设周数是一年中的实际周数,而不是数组中的某些索引。 I'd also not use the same data structure but am adapting so that you won't need to change your structure. 我也不会使用相同的数据结构,而是会进行调整,以使您无需更改结构。

var arr = [
    [{
        "2017-01-05": 607
    }, {
        "2017-01-06": 430
    }, {
        "2017-01-07": 357
    }, {
        "2017-01-08": 277
    }],
    [{
        "2017-01-09": 607
    }, {
        "2017-01-10": 430
    }, {
        "2017-01-11": 357
    }, {
        "2017-01-12": 277
    }]
]

function week(d) {
    // taken from http://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php
    d.setHours(0, 0, 0, 0);
    d.setDate(d.getDate() + 4 - (d.getDay() || 7));
    return Math.ceil((((d - new Date(d.getFullYear(), 0, 1)) / 8.64e7) + 1) / 7);
}

var weeks = {};
for (var x in arr) {
    var subarr = arr[x]
    for (var y in subarr) {
        var obj = subarr[y];
        for (var when in obj) {
            var d = new Date(when);
            var which_week = "Week " + week(d);
            if (which_week in weeks) {
                weeks[which_week] += obj[when];
            } else {
                weeks[which_week] = obj[when];
            }
        }
    }
}

without forEach 没有forEach

var arrs = [
  [{
    "2017-01-05": 607
  }, {
    "2017-01-06": 430
  }, {
    "2017-01-07": 357
  }, {
    "2017-01-08": 277
  }],
  [{
    "2017-01-09": 607
  }, {
    "2017-01-10": 430
  }, {
    "2017-01-11": 357
  }, {
    "2017-01-12": 277
  }]
];

function loop1(arr, i, r){
    r = (r ? r : 0) + arr[i][Object.keys(arr[i])[0]];
    return ++i == arr.length ? r : loop1(arr, i, r);
}
function loop2(arrs, i, r){
    i = i ? i : 0;
    r = r ? r : {};
    r['Week ' + (i + 1)] = loop1(arrs[i], 0);
    return ++i == arrs.length ? r : loop2(arrs, i, r);
}

var newArr = loop2(arrs);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM