Is there a more elegant way to set multiple keys of an array in JavaScript?
The current code may not be aesthetically pleasing, but it seems to be the only solution that works.
var listData = [];
listData['today'] = [];
listData['data1'] = [];
listData['data2'] = [];
listData['data3'] = [];
listData['data4'] = [];
listData['data5'] = [];
listData['data6'] = [];
listData['data6'] = [];
I attempted to initialize the array in a different way:
function initArray(arr, keys, defaultValue) {
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
delete arr[key];
arr[key] = defaultValue;
}
return arr;
}
However, after setting the array, I encountered issues when adding data:
When trying to insert data into the array using commands like listData['data1'].push(datalist[i].num) listData['data2'].push(datalist[i].num), the resulting array displayed duplicates of 'data1' and 'data2' entries.
If anyone has suggestions on how to efficiently add keys to an array, I would greatly appreciate the assistance.