主干模型循环输出最终对象,而不是迭代器



因此,我试图用五行来填充数组"cat",这将保持每个类别在一个步骤中出现的次数的连续计数。问题是循环执行了,但它没有一个接一个地输出结果,所以我可以看到每个类别的计数是如何上升的(时间序列)。它所做的一切都吐出了每个类别的总数

            this.cat = [];
            this.cat[0] = 0;
            this.cat[1] = 0;
            this.cat[2] = 0;
            this.cat[3] = 0;
            this.cat[4] = 0;
            this.total = 0;
        },
        model: vote,
        parse: function(data)
        {
            data = data.response ? data.response : data;
            if(data.length == 0)
                return;
            for (var i = 0; i < data.length; i++) {
                this.itemParse(data[i]);
            };
            return data;
        },
        itemParse: function(item){
            this.cat[item.user_vote_index]++;
            this.total++;
            console.log(this.cat);
            item.cat = this.cat;
            item.total = this.total;
            console.log(item);
            return item;
        }
    })
})`

这是我操作时的控制台日志console.log(this.cat); console.log(this.cat);[1, 0, 0, 0, 0] stats.js:33

[1, 0, 0, 1, 0] stats.js:33

[1, 0, 1, 1, 0] stats.js:33

直到

[8, 6, 1, 2, 1]

这就是我希望数据存储的方式(一次一次迭代);但是,当我控制台记录集合项时,.cat会为每行提供[8,6,1,2,1]

您必须修改itemParse函数:

itemParse: function(item){
  this.cat[item.user_vote_index]++;
  this.total++;
  // here you have to clone the array instead of creating a reference to it
  item.cat = _.clone(this.cat);
  item.total = this.total;
  return item;
}