How do I add an array to array with variables & functions?

var ranges = new Array();
fulldate='2012/06/11:2012/10/23|2012/03/11:2012/05/23'.split('|');

for(var i=0; i<fulldate.length; i++) {
    adate=fulldate[i].toString().split(':');

    startdate=adate[0].toString().split('/');
    enddate=adate[1].toString().split('/');

    //***This area****************************
    ranges.push = ({ start: new Date(startdate[0],startdate[1]-1,startdate[2]), end: new Date(enddate[0],enddate[1]-1,enddate[2]) });
    //***This area****************************
}
share|improve this question
feedback

2 Answers

up vote 3 down vote accepted

push is a method, you have to use it like this:

ranges.push({ start: new Date(startdate[0],startdate[1]-1,startdate[2]), end: new Date(enddate[0],enddate[1]-1,enddate[2]) });
share|improve this answer
feedback

To be persnickety, you aren't really trying to add an array to another. For that you can just use array.concat():

var cArray = aArray.concat(bArray).

It looks like you want to transform one array into another. You could use array.map():

var fulldate='2012/06/11:2012/10/23|2012/03/11:2012/05/23'.split('|');
var ranges = fulldate.map(function(x) {
    var adate=x.toString().split(':');
    var startdate=adate[0].toString().split('/');
    var enddate=adate[1].toString().split('/');
    return { 
        start: new Date(startdate[0],startdate[1]-1,startdate[2]),
        end: new Date(enddate[0],enddate[1]-1,enddate[2])
    };
});
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.