我需要一个结构,其中每个键(日期)都保存一个整数数组。 我已经尝试过以下方法,但它似乎不起作用。 我用数组创建一条记录,然后将其所有值设置为 0,尽管它们看起来仍然是 NaN:
第一条语句创建一条日期记录,并带有由变量定义长度的关联数组。
Dictionary.set(Meteor.jira.formatDate(moment(date),[arrayLenght]);
//initialize the array with 0 values
var i;
for (i = 0; i < arrayLenght; i++) {
Dictionary.set(Meteor.jira.formatDate(moment(date))[i]=0);
}
}
最佳答案
先填充数组,然后放入字典。
您可以通过更改包含数组引用的任何变量来更新其值。
let Dictionary = new Map();
let arrayLenght = 6;
let key = "2018-11-15" //Meteor.jira.formatDate(moment(date))
let arr = Array(arrayLenght).fill(0);
Dictionary.set(key, arr);
let value = Dictionary.get(key);
console.log(JSON.stringify(value));
value[2] = 1;
value.unshift(33);
arr.unshift(15);
console.log(JSON.stringify(Dictionary.get(key)))
关于javascript - 映射每个日期保存一个数组,实现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53319993/