在我的JS代码中找不到bug

时间:2015-12-23 15:04:44

标签: javascript arrays

我正在尝试创建一个最终数组,该数组包含数月的对象总数,并合并相同的月份并将其itemCount求和。

Z阵列来自Chrome的控制台:

0: Object
itemCount: 6
month: "Aug"

1: Object
itemCount: 0
month: "Jun"

2: Object
itemCount: 0
month: "Sep"

3: Object
itemCount: 0
month: "Sep"    

和我写的代码在几个月内汇总itemCount并合并,如果有相同的月份:

let counterCurrItem = 0, month, 
        finalArr = [], itemCount = 0

    for (i = 0; i < Z.length; i++) {
        month = Z[i].month
        itemCount = Z[i].itemCount
        if (Z[i + 1] !== undefined) {
            if (month == Z[i + 1].month) {
                counterCurrItem = counterCurrItem + itemCount
            } else {
                finalArr.push([counterCurrItem, month])
            }
        } else {
            finalArr.push([counterCurrItem, month])
        }
    }

然而,最终数组没有保持预期的值,我希望它的第一个数组为[6,'Aug'],但它是[0,'Aug']。

那么问题在于我的代码?

一些注意事项:月份总是按字母顺序排序,Z数组总是包含多个对象。

7 个答案:

答案 0 :(得分:2)

您添加finalArr.push([counterCurrItem, month])之类的项目,但变量counterCurrItem已添加到if语句中,因此它永远不会增加;

此外,您应该在循环中而不是在循环外部创建临时变量,这样您就不会得到不正确的数据。

做出最少的改变:

let counterCurrItem = 0, month, 
    finalArr = [], itemCount = 0

for (i = 0; i < Z.length; i++) {
    month = Z[i].month
    itemCount = Z[i].itemCount
    if (Z[i + 1] && month == Z[i + 1].month ) {
        counterCurrItem = counterCurrItem + itemCount
        finalArr.push([counterCurrItem, month])
    } else {
        finalArr.push([itemCount, month])
    }
}

答案 1 :(得分:1)

  if (month == Z[i + 1].month) {
                counterCurrItem = counterCurrItem + itemCount
            } else {
                finalArr.push([counterCurrItem + itemCount, month]) <<<<
            }

逻辑问题

答案 2 :(得分:1)

我认为问题在于:

1)

if (month == Z[i + 1].month)

您只将当前元素的月份与下一个月份进行比较。您应该将它与所有下一个元素进行比较。

2)在if-else语句中定义counterCurrItem。在if-else之前定义它。

答案 3 :(得分:1)

使用缩减的替代解决方案:

var monthMap = Z.reduce(function ( acc, record ) {
        if (!acc[record.month]) acc[record.month] = 0;
        acc[record.month] += record.itemCount;
        return acc;
    }, {}),
    finalAry = Object.keys(monthMap).reduce(function ( ary, key ) {
        ary.push([monthMap[key], key);
        return ary;
    }, []);

答案 4 :(得分:1)

好的,请仔细阅读代码。

得到第1项[6,6月]。

month = Z[i].month //this is Aug
itemCount = Z[i].itemCount // This is 6

Z[i + 1] // This is not undefined

month == Z[i + 1].month // Aug is not equal to Jun

所以它打电话:

finalArr.push([counterCurrItem, month]) // counterCurrItem is not defined so will become 0

希望有意义

答案 5 :(得分:1)

问题是你没有连续两个月没有设置counterCurrItem(例如在aug中),所以它会读为0,因为它是初始化的。

// change this
if (month == Z[i + 1].month) {
    counterCurrItem = counterCurrItem + itemCount
} else {
    finalArr.push([counterCurrItem, month])
}

// to this:          
if (month == Z[i + 1].month) {
    counterCurrItem = counterCurrItem + itemCount
} else {
    finalArr.push([itemCount, month]); // changed!!
}])

答案 6 :(得分:1)

我创建了一个脚本,无论月数相等还是排序,它都能正常工作。

解释在代码注释中:

Z = [
  {itemCount: 1, month: 'Sep'},
  {itemCount: 6, month: 'Aug'},
  {itemCount: 0, month: 'Jun'},
  {itemCount: 2, month: 'Sep'}
];

let counterCurrItem = 0,
  month,
  finalArr = [],
  searchArr = [], // search array, will hold objects with their month as a key
  itemCount = 0;

// Loop over all elements in the original array
Z.forEach(function(obj, i) {
  // Get the current object's properties
  month = obj.month;
  itemCount = obj.itemCount;

  // See if the current month has been added to the search array
  if (searchArr[month] === undefined) {
    // First time we see this month, so add it to the final array and the search array
    finalArr.push(obj);
    searchArr[month] = obj;
  }
  else {
    // We had this month before, so loop over the final array, find the month, and update it's count
    for (var j = 0; j < finalArr.length; j++) {
        if (finalArr[j].month === month) {
        finalArr[j].itemCount += itemCount;
      }
    }
  }
});

console.log(finalArr);

小提琴: https://jsfiddle.net/97y988m2/4/