简体   繁体   中英

add array to array in javascript

I have array items as my code below, I want to push these Array items "keywords_name" or "keywords_name" into Chart.js library that i used. I can use as what i did below by calling each array item in "labels" or "dataset" but i guess thats should not be the way.

// Top keywords.
            var keywords_name = [];
            var keywords_num = [];

            for(i = 1; i<=10; i++){
                keywords_name.push($("#content .box_segment:eq(1) table tbody tr:nth-child("+i+") td:first").text());
                keywords_num.push(parseInt($("#content .box_segment:eq(1) table tbody tr:nth-child("+i+") td:last").text()))
            }

            var bardata = {
                labels : [keywords_name[1], keywords_name[2], keywords_name[3], keywords_name[4]],
                datasets : [
                    {
                        fillColor : "rgba(220,220,220,0.5)",
                        strokeColor : "rgba(220,220,220,1)",
                        data : [1,2,3,4]
                    }
                ]
            }

Why don't you just assign the obtained arrays to the new bardata object like this?:

var bardata = {
               labels : keywords_name,
               datasets : {
                           fillColor : "rgba(220,220,220,0.5)",
                           strokeColor : "rgba(220,220,220,1)",
                           data : keywords_num
                          }
              }

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