Matt Hutch Matt Hutch - 2 months ago 8
jQuery Question

Adding ' to the start and end of each array value

I'm trying to add a ' to the end and the start of each array value. The reason for this is that I have a graph JQuery set up and want to manipulate the chart value names. I can do perfectly if I wanted them to be numbers however whenever I try using text I get an error, which I believe is because there aren't any ' at the start and end of my array values. Any ideas guys?

The error i'm getting is


Uncaught ReferenceError: MyTextValue is not defined


var barNames = [];

if (NoOfBars >= 1){
barNames = [name1];
}
if (NoOfBars >= 2){
barNames = [name1, name2];
}
if (NoOfBars >= 3){
barNames = [name1, name2, name3];
}
if (NoOfBars >= 4){
barNames = [name1, name2, name3, name4];
}
if (NoOfBars >= 5){
barNames = [name1, name2, name3, name4, name5];
}
if (NoOfBars >= 6){
barNames = [name1, name2, name3, name4, name5, name6];
}
if (NoOfBars >= 7){
barNames = [name1, name2, name3, name4, name5, name6, name7];
}
if (NoOfBars >= 8){
barNames = [name1, name2, name3, name4, name5, name6, name7, name8];
}
if (NoOfBars >= 9){
barNames = [name1, name2, name3, name4, name5, name6, name7, name8, name9];
}
if (NoOfBars >= 10){
barNames = [name1, name2, name3, name4, name5, name6, name7, name8, name9, name10];
}

var bar = new RGraph.Bar({
id:'cvs1',
data: barValue,
options: {
backgroundGridDashed: true,
labels: barNames,
title: 'Title of Chart',
strokestyle: 'rgba(0,0,0,0)',
textAccessible: true
}
}).draw();

Answer

Something like...

barNames = [
    "name1", "name2", "name3", "name4", "name5",
    "name6", "name7", "name8", "name9", "name10"
].slice(0,NoOfBars);

It really depends on how these values are used.