Recently, I took on the challenge of learning Javascript independently and came across a rather complex task.
I have an array with 18 labels and another 1D array containing all the values. Each label index corresponds to every nth element in the array.
For example, if the label index is 0, then the first and nineteenth elements belong to label 1, while the second and twentieth elements belong to the label at index 2.
I managed to create a script that constructs an Object and appends the values from the correct indices. However, I wonder if there are more efficient ways to map values between these two arrays?
var labelArrayLength = 18;
var i = 0, sampleArray = [];
while (i < 6642) {
sampleArray.push(i);
i++;
};
var j = 0, myObject = {};
while (j < labelArrayLength) {
myObject[j] = {label:`dummyLabel${j}`, data:[]};
j++
};
var stepSize = sampleArray.length / labelArrayLength;
var counter = 0;
for (var k = 0; k < stepSize; k++) {
for (var b = 0; b < labelArrayLength; b++) {
myObject[b]['data'].push(sampleArray[counter]);
counter++;
};
};