循环遍历包含object的数组数组,并以JavaScript / Angular更新数组

时间:2017-01-12 11:45:48

标签: javascript angularjs arrays

我试图在对象[[{},{},{}],[{},{},{}]]的数组数组中循环,并创建一个基本上"总计"的新数组。数组。我对如何实现这一点感到有点不知所措。

我的数据如下所示:

[
  [{
    "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
  }]
],

我想"算上"和"标签"每周,每周总计。示例:

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

我知道我应该使用forEach循环,但它会有点粗略:

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

非常感谢您的帮助和指导。

4 个答案:

答案 0 :(得分:3)

我会使用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);

答案 1 :(得分:2)

你可以尝试这样的事情。



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)




注意,我建议您使用对象而不是对象数组。这样做的好处是你不需要循环输出来获得价值。您可以直接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)




答案 2 :(得分:1)

变量周应该保持你想要的...... 我假设周数是一年中的实际周数,而不是数组中的某个索引。我也没有使用相同的数据结构,但我正在进行调整,以便您不需要更改结构。

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];
            }
        }
    }
}

答案 3 :(得分:0)

没有 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);