简体   繁体   中英

Displaying array data in javascript

I have a javascript code which looks like this

 var startdate = "06/10/2014";
            //var newdate = new Date();
            var num = 12
            var months = [];
            for (var a = 0; a < num; a++) {
                var dt = new Date(startdate);
                dt.setMonth(dt.getMonth() - a);
                months[a] = dt;
            }
            return months;

             arylist = arylist + '{';

             arylist = arylist + '"name" :"' + months + '",';

             arylist = arylist + '"st_time_am_pm" :"' + st_time_am_pm + '",';
            arylist = arylist + '"ed_time_am_pm" :"' + ed_time_am_pm + '",';

            arylist = arylist + '}]';

for loop retuns same data which is 12 months back based on inserted date . I want to display all the results in '"name" :"' + months + '", here and it will display it in the arrylist. But it just passes that part .I don't know where the issue is . Any help appreciates . Thanks

try this:

var startdate = "06/10/2014";
        //var newdate = new Date();
        var num = 12
        var months = [];
        var dt = new Date(startdate);
        for (var a = 0; a < num; a++) {
            dt.setMonth(dt.getMonth() - a);
            months[a] = dt;
        }

         arylist = arylist + '{';

         arylist = arylist + '"name" :"' + months + '",';

         arylist = arylist + '"st_time_am_pm" :"' + st_time_am_pm + '",';
        arylist = arylist + '"ed_time_am_pm" :"' + ed_time_am_pm + '",';

        arylist = arylist + '}]';

        return months;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM