简体   繁体   English

在JavaScript中将数组添加到数组

[英]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. 我在下面的代码中有数组项,我想将这些数组项“ keywords_name”或“ keywords_name”推送到我使用的Chart.js库中。 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?: 您为什么不像这样将获取的数组仅分配给新的bardata对象?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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