我正在尝试将一个数组插入另一个数组。
我举了一些例子。 每列都有代码和记录。记录将是包含日期和计数等字段的数组。
var column = [{code:"", record:0}];
var row = [{date:"", count: 0}];
column.length = 0
row.push({date:"2019-01-02", count:1});
row.push({date:"2019-01-03", count:1});
column.push({code:"Home", record:row});
row.length = 0;
row.push({date:"2019-01-04", count:2});
row.push({date:"2019-01-05", count:2});
column.push({code:"menu1", record:row});
row.length = 0;
row.push({date:"2019-01-06", count:3});
row.push({date:"2019-01-07", count:3});
column.push({code:"menu2", record:row});
我预期输出如下
column[0].code: "Home"
column[0].record[0].date:"2019-01-02", column[0].record[0].count:1
column[0].record[1].date:"2019-01-03", column[0].record[1].count:1
column[1].code: "menu1"
column[1].record[0].date:"2019-01-04", column[1].record[0].count:2
column[1].record[1].date:"2019-01-05", column[1].record[1].count:2
column[2].code: "menu2"
column[2].record[0].date:"2019-01-06", column[2].record[0].count:3
column[2].record[1].date:"2019-01-07", column[2].record[1].count:3
实际结果
column[0].code: "Home"
column[0].record[0].date:"2019-01-06", column[0].record[0].count:3
column[0].record[1].date:"2019-01-07", column[0].record[1].count:3
column[1].code: "menu1"
column[1].record[0].date:"2019-01-06", column[1].record[0].count:3
column[1].record[1].date:"2019-01-07", column[1].record[1].count:3
column[2].code: "menu2"
column[2].record[0].date:"2019-01-06", column[2].record[0].count:3
column[2].record[1].date:"2019-01-07", column[2].record[1].count:3
由于某种原因,记录的现有值将替换为上次推送记录的值。 有什么办法可以达到预期的结果吗?