This is the teacher's solution to a parsing problem. I understand all of the code until it reaches the point with the forEach. Could someone explain that for me? Specifically the obj[headers[idx]] = val.
let csvData = "name,age\nFrodo,50\nSam,38\nMerry,36\nPippin,26";
//Headers: name, age.
//Row example: Frodo, 50
function csvConverter(data) {
//Split csvData into rows based on each newline character.
let rows = csvData.split('\n');
//Split the first row or headers into individual strings based on
//the comma character.
let headers = rows[0].split(',') //[name, age]
let result = [];
//Iterate over every content row.
for (let i = 1; i < rows.length; i++) {
let obj = {};
let data = rows[i].split(',') // Ex. [ 'Frodo', 50 ]
data.forEach((val, idx) => {
obj[headers[idx]] = val
})
result.push(obj);
}
return result
}
console.log(csvConverter(csvData));