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]
}
]
}
asked Apr 16, 2013 at 7:48
max li
2,4674 gold badges31 silver badges46 bronze badges
-
2why dont you send your array directly ? { labels : keywords_name }Parthik Gosar– Parthik Gosar2013年04月16日 07:51:41 +00:00Commented Apr 16, 2013 at 7:51
-
you are right, my mistake!max li– max li2013年04月16日 07:54:02 +00:00Commented Apr 16, 2013 at 7:54
1 Answer 1
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
}
}
Sign up to request clarification or add additional context in comments.
Comments
lang-js