[英]How to put a list of data from a for loop into a single array? i'm getting [item] [item] [item] this kind of variable…not [item, item, item]
function chartFunkcio(event) {
let localData;
if (localStorage.getItem('localData') === null) {
localData = [];
} else {
localData = JSON.parse(localStorage.getItem('localData'));
}
//get objects from array between mentioned dates
var result = localData.filter(function (obj) {
return obj.date >= fromdate.value && obj.date <= todate.value;
});
var filtered = result;
//Filtered object from array are sorted after dates
var sortedd = filtered.sort(function (a, b) {
var c = new Date(a.date);
var d = new Date(b.date);
return c - d;
});
for (i = 0; i < sortedd.length; i++) {
var datesforchart = [];
const pushedarray = datesforchart.push(sortedd[i].date);
console.log(pushedarray);
}
}
The local storage data is a tables data.本地存储数据是表格数据。 I filtered that data after dates and then i sorted those dates.我在日期之后过滤了该数据,然后对这些日期进行了排序。 the sorted date are the one i would like to get in a single array like below mentioned.排序后的日期是我想在单个数组中获取的日期,如下所述。 i'm getting [item] [item] [item] this kind of data in console log...not an array like this[item, item, item] i would need that array for implementing a chart, and for that i would neet that the array to look like this ["item", "item2", "item3"]我在控制台日志中得到 [item] [item] [item] 这种数据...不是这样的数组 [item, item, item] 我需要该数组来实现图表,为此我会确保数组看起来像这样 ["item", "item2", "item3"]
There's something wrong inside the for
loop. for
循环内部有问题。
for (i = 0; i < sortedd.length; i++) {
var datesforchart = [];
const pushedarray = datesforchart.push(sortedd[i].date);
console.log(pushedarray);
}
If you try to get new array with just date
elements, you can replace the for
loop with below code which uses map
function.如果您尝试仅使用date
元素获取新数组,则可以使用以下代码替换for
循环,该代码使用map
function。
var datesforchart = sortedd.map(function (item) {
return item.date
});
console.log(datesforchart)
// for (i = 0; i < sortedd.length; i++) {
// var datesforchart = [];
// const pushedarray = datesforchart.push(sortedd[i].date);
// console.log(pushedarray);
// }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.