我正在尝试用对象填充数组,但是该数组无法正确填充。在数组的所有位置都设置了最后一个值。这是代码:
form
Im Getting数组看起来像这样在所有位置仅填充1个值:
var matrixprice = 5;
var qualifiedDate = '2019-10-01';
var today = new Date();
var qDate = new Date(qualifiedDate);
var nextDay = qDate;
var myObject = new Object();
var myArray = [];
var dailybonus = matrixprice * 0.03;
var full_bonus = matrixprice * 2;
var i = 0;
while (i <= full_bonus) {
nextDay.setDate(nextDay.getDate() + 1);
i += dailybonus;
myObject.title = '$' + i;
myObject.start = nextDay;
myArray.push(myObject);
}
var myString = JSON.stringify(myArray);
console.log(myString);
提前谢谢!
答案 0 :(得分:1)
更新while循环以推送新对象:
while(i <= full_bonus){
nextDay.setDate(nextDay.getDate()+1);
i += dailybonus;
myArray.push({title: '$'+i, start: new Date(nextDay)});
}
这是完整的有效示例:
var matrixprice = 5;
var qualifiedDate = '2019-10-01';
var today = new Date();
var qDate = new Date(qualifiedDate);
var nextDay = qDate;
var myArray = [];
var dailybonus = matrixprice * 0.03;
var full_bonus = matrixprice * 2;
var i = 0;
while(i <= full_bonus){
nextDay.setDate(nextDay.getDate()+1);
i += dailybonus;
myArray.push({title: '$'+i, start: new Date(nextDay)});
}
var myString = JSON.stringify(myArray);
console.log(myString);