I have a problem merging 2 arrays in an for loop see code:
function count_days(tijden,datums ){
var length = tijden.length;
element = null;
var similarities =0;
var backup = [];
var dateArray =[];
for (var i = 0; i <length; i++) {
element = tijden[i];
tijden[i] = element;
dateArray = getDates(new Date(tijden[i]['begin']), new Date(tijden[i]['eind']));
}
The problem is that when the code is running i get one array for each loop. I need to get one array with the data from all loops. I tryde concat but didnt get this to work
It should be...
dateArray.push(
getDates(new Date(tijden[i]['begin']), new Date(tijden[i]['eind']))
);
... or, if getDates
method returns an array and you want dateArray
to be flattened:
Array.prototype.push.apply(dateArray,
getDates(new Date(tijden[i]['begin']),
new Date(tijden[i]['eind']))
);
As it stands now, you just rewrite dateArray
at each step of for
loop.
A sidenote: this...
element = tijden[i];
tijden[i] = element;
... makes exactly zero sense: at least one of these lines (most probably the latter) is redundant. Remove this - and code becomes easier to read:
var element, currDates;
for (var i = 0, i < length; i++) {
element = tijden[i];
currDates = getDates(new Date(element.begin), new Date(element.eind));
Array.prototype.push.apply(dateArray, currDates);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.