Reputation: 1
i am trying to building dynamically an array of empty elements. This is what i have tried so far.
aData.length=5;
var noOFSeries = [];
for (var i = 1; i <= aData.length - 1; i++) {
noOFSeries.push([]);
}
I want this kind of output [], [], [], []
so that i can put in jqplot.
$.jqplot('barchartReqCat', [chartData, [], [], [], []], options);
replace to
$.jqplot('barchartReqCat', [chartData, noOFSeries], options);
Thanks for your help.
Upvotes: 0
Views: 58
Reputation: 1513
There you have some ways:
arr1
filled with .map();
arr2
filled with .fill(); and
arr3
with loop.
var arr1 = [1, 2, 3, 4].map(function(value) {
return [];
});
console.log(arr1);
var arr2 = new Array(4).fill([]);
console.log(arr2);
var arr3 = [];
var length = 5;
for(var i = 0; i < 4; i++) arr3.push([]);
console.log(arr3);
Upvotes: 2
Reputation: 5323
You could use spread operator or, if you want backward compatibility, you can use a new array (myArr
in my example):
var aData = {
length: 5
},
noOFSeries = [],
chartData = [1, 2, 3, 4, 5]
for (var i = 1; i <= aData.length - 1; i++) {
noOFSeries.push([]);
}
$.jqplot('barchartReqCat', [chartData, ...noOFSeries], options);
var myArr = [chartData].concat(noOFSeries);
$.jqplot('barchartReqCat', myArr, options);
Upvotes: 1
Reputation: 3879
You can use Array.prototype.fill
like this:
let noOfSeries = Array(5).fill([]);
Upvotes: 2