Upon taking over a codebase, I was greeted with this particular coding style devoid of any tests:
var series1 = [];
var series2 = [];
var series3 = [];
var series4 = [];
var series5 = [];
var series6 = [];
var series7 = [];
var series8 = [];
for (var y = 1; y <= seriesData.length; y++) {
// columns are series
eval("series" + y).push({
label: "series" + y,
lineColor: colorArr[seriesData[y - 1].colorIndex],
x: sampleTime,
y: rows[x][seriesData[y - 1].index]
});
}
An issue has arisen as we plan to accommodate more than just 8 sets of data. Personally, the existing code style irks me and there are concerns surrounding the use of the eval
function in JavaScript. Is there a more efficient way to revamp this?
My attempted solution so far:
let multiarr = []
for (var y = 1; y <= seriesData.length; y++) {
// columns are series
let arr = [];
arr.push({
label: "series" + y,
lineColor: colorArr[seriesData[y - 1].colorIndex],
x: sampleTime,
y: rows[x][seriesData[y - 1].index]
});
}
multiarr.push(arr);