I'm attempting to use Javascript to lookup a value in a CSV file and return it's corresponding value (basically what a VLOOKUP does in Excel)
I've been able to follow some examples separately of pulling the CSV data into an array, and I've seen some examples of looking up data in arrays - but for the life of me I can't figure out how to get both working.
For example, the CSV file stations.csv has the following data:
mac,name
69167f276e9g,LINE1
69167f276e9f,LINE2
What I want to be able to do is lookup the 'mac' value from the CSV, and return the corresponding 'name' value.
So if I look for '69167f276e9f' I want to get the value LINE2 back.
[EDIT: Adding the code I've tried using MauriceNino's suggestion - but getting error Uncaught TypeError: Cannot read property '1' of undefined at the line 'return result[1];' ]:
$.ajax('stations.csv').done(function(data) {
const lookup = (arr, mac) => {
let twoDimArr = dataArr.map(el => el.split(',')); // map it to array of arrays of strings (data)
let result = twoDimArr.filter(el => el[0] == mac)[0]; // Get the first element where the mac matches the first element in the array
return result[1]; // Return the second element in the array
};
let dataArr = data.split('\n');
dataArr .shift(); // Remove the first array element (The header)
let resultMac = lookup(dataArr, "69167f276e9f");
console.log(resultMac);
})