I need to create this data structure in Javascript:
var bar_data = {
data: [
[sistOper[0], tot[0]],
[sistOper[1], tot[1]],
[sistOper[2], tot[2],
[sistOper[3], tot[3]]
],
color: '#3c8dbc'
}
It works correctly, but if I have an array with 20 elements this syntax is very inefficient.
I’ve tried this, but doesn’t work:
var bar_data = {
data: [[ sistOper, tot ]],
color: '#3c8dbc'
}
Any suggestions?
2
Answers
You can use Javascript’s hacky-looking zipping:
Or, if you’re already using lodash, you can use
_.zip
."Zipping" is the term you’re looking for though.
Use a
for
loop to construct thedata
array: