I loop through an array called accData and make a new array called WL and RF. I want to make it easy to maintain, but I have a problem. When I want to change the WL array data to CH2 and RF array data to DIG2, I have to type the script manually. This is my script
const accData = [
{ st: 'serijabo', path: 'serijabo.json', wl: 'CH1', rf: 'DIG1' },
{ st: 'sukabumi', path: 'sukabumi.json', wl: 'CH2', rf: 'DIG2' },
];
for (let i = 0; i < accData.length; i++) {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
const data = JSON.parse(xhr.responseText);
let WL = [];
let RF = [];
for (let i = 0; i < data.length; i++) {
WL.push(Number(data[i].CH1)); // this line
RF.push(Number(data[i].DIG1)); // and this line
}
}
}
}
I try to use ES6 template to solve it but it is not working because the template must be place in a string.
WL.push(Number(data[i].`${accData[i].wl}`)); //not working
RF.push(Number(data[i].`${accData[i].rf}`)); //not working
How can I solve this?