【发布时间】:2018-12-10 05:45:27
【问题描述】:
我正在创建 2 个给定日期之间的天数组,键的格式应为 DD/MM/YYYY,值应为数字(为每个日期设置价格)
这似乎有效,因为数组包含我给它的值(通过日期选择器),但我不能循环遍历这个数组,可能是因为它的长度返回 0,即使它包含元素
这是控制台日志语句的截图
这是创建数组的代码
var arrayOfDatesBetween = new Array();
// daysBetween = integer representing the count of days between the chosen dates
for (let i = 0; i < daysBetween; i++) {
// just add one day on each iteration but keep count of the first
let q = i === 0 ? i : 1;
let _date = _dateIn.setDate(_dateIn.getDate()+q);
// lcsgDate() formats the date as I need it: DD/MM/YYYY
let __date = lcsgDate(_date);
// getDatePrice() gets the price for the given date by searching into another Array of date:price
arrayOfDatesBetween[__date] = getDatePrice(__date);
}
// result
console.log(arrayOfDatesBetween);
【问题讨论】:
-
"10/07/2018"不是整数索引。 -
如果有键,就不是数组;数组有索引。
-
考虑将格式改为
[{'date':'09/07/2018','price':'44'},{'date':'09/11/2018','price':'45'}],这样更容易遍历 -
@melpomene 所以我可以用一个对象来做吗?对象有键
标签: javascript arrays date