I am using the Samsung Tizen SDK for SmartTV app development. I have some code that converts CSV to a Javascript object. I am having problems accessing one of the keys in the object that is created from the code.
Code:
function csvJSON(csv) {
var lines = csv.split("\n");
var result = [];
var headers = lines[0].split(",");
for (var i = 1; i < lines.length; i++) {
var obj = {startTime:'',
endTime:'',
day:''};
var currentline = lines[i].split(",");
for (var j = 0; j < headers.length; j++) {
obj[headers[j]] = currentline[j];
}
result.push(obj);
}
return result; // JavaScript object
}
My inputs to this function look like: Input to function
While debugging the return result line in the console developer mode (I set a local watch of obj.endTime), I cannot access the endTime key, despite it showing up in the debugger. It is almost as if a special character is involved somehow.
I tried the same snippet of code in jsFiddle and it worked ... so it seems like something related to the version of Javascript/ECMAScript that is running on the Tizen Emulator. Perhaps this was an issue in earlier versions of Javascript ?
Thanks!
I would check that endTime
in your csv header is truly equal to endtime
, i.e.
endTime1 = 'endTime'
endTime2 = '\uff45ndTime'
console.log(`${endTime1} equals ${endTime2} is ${endTime1 === endTime2}`)
// endTime equals endTime is false
obj = {}
obj[endTime1] = 'endTime1'
obj[endTime2] = 'endTime2'
console.log(obj)
// {
// "endTime": "endTime1",
// "endTime": "endTime2"
// }
Any reason why you are not using a library for this?, e.g. http://papaparse.com/docs#csv-to-json