I just tried to make one function that will take CSV files and return JSON data. when I console out the data it will work fine but when I tried to return the same data it will return undefined.
var fs = require('fs');
//sorting the json...
function compareStrings(a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
return (a < b) ? -1 : (a > b) ? 1 : 0;
}
//reding csv and converting it into json..........
var csv_to_json = ()=>{
fs.readFile('./Data/book.csv',(err,data)=>{
let jsonData = [];
var content = data.toString().split('\n').join().split('\r');
var key = content[0].split(';');
content.shift();
content.forEach((item)=>{
var temp = item.replace(",").split(';');
var data = {}
for(var i=0;i<key.length;i++){
data[key[i]] = temp[i]
}
jsonData.push(data);
})
jsonData.sort(function(a, b) {
return compareStrings(a.title, b.title);
})
// console.log is working fine but when I tried to return the same data it won't work properly
// console.log(jsonData);
return jsonData;
})
}
console.log(csv_to_json())