Converting CSV data to a list of objects Javascript - javascript

I wish to convert a CSV file with data on camping spots to a list of objects. The attributes of the shelters are separated by a semicolon and looks something like this:
X;Y;postnumber_city;name;postnumber;describtion_short;max_number_of_people
445502.7247;6212415.4577;Hvide Sande;Shelter 1;6960;3 shelters, grillplads, borde-/bænkesæt og toilet;6
My code looks like this:
const fs = require('fs');
function csvToObjects(fileName) {
fs.readFileSync(fileName, "utf-8");
let jsObj = [];
let shelters = fileName.split('\n')
for (let attributes of shelters) {
attributes = shelters.split('; ');
}
obj.push(attributes)
}
console.log(csvToObjects('shelters.csv'));
I really don't know what I am doing wrong. I get an error from attributes = shelters.split('; ');
TypeError: shelters.split is not a function

You need to put obj.push(attributes) inside for loops,and use attributes.split('; '); instead of shelters.split('; ')
function csvToObjects(fileName) {
fs.readFileSync(fileName, "utf-8");
let jsObj = [];
let shelters = fileName.split('\n')
for (let attributes of shelters) {
let data = attributes.split(';');
jsObj.push(data)
}
}

I attempted with some other for-loops and assigned a variable for my readfile, which seemed to do the job.
const fs = require('fs');
function csvToObjects(fileName) {
let file = fs.readFileSync(fileName, "utf-8");
let jsObj = [];
let shelters = file.split('\n')
let attributes = shelters[0].split(';')
for (let i = 0; i < shelters.length; i++) {
let data = shelters[i].split(';')
let obj = {};
for (let j = 0; j < data.length; j++) {
obj[attributes[j].trim()] = data[j].trim();
}
jsObj.push(obj);
}
return jsObj;
}

Related

JavaScript Add item to array if its not already exist

How can I check in JavaScript if the array already have the item?
Im adding the items to my array with the next code:
const booked_hours = [];
for (let i = 0; i < apptid_set.size; i++) {
const book_hours = [...book_times][i].split(" ");
booked_hours.push(book_hours[2]);
}
//alert(booked_hours);
Its works well, just in the booked_hours shouldnt be any duplicated item.
How can I do that?
Without knowing what you're adding to the booked_hours array this answer might have to be tweaked but take a look at this.
const booked_hours = [];
for (let i = 0; i < apptid_set.size; i++) {
const book_hours = [...book_times][i].split(" ");
if (!booked_hours.includes(book_hours[2])) {
booked_hours.push(book_hours[2]);
}
}
You can use a set instead of an array for booked_hours
const booked_hours = new Set();
for (let i = 0; i < apptid_set.size; i++) {
const book_hours = book_times[i].split(' ');
booked_hours.add(book_hours[2]);
}
And to convert it back to an array you can do [...booked_hours]

js open txt file read line by line and turn it to an array

The problem is that I want to use the array elsewhere in my code. But I do not know how to access it?
Is there any other way to read the file and turn it into an array that I can use?
var dir = 'file.txt';
function file() {
const fileUrl = dir;
fetch(fileUrl)
.then( response => response.text() )
.then( text => readFile(text) )
}
function readFile(text) {
var lines = text.split('\n');
var array = [];
var str;
for (var i = 0; i < lines.length; i++) {
var str = lines[i];
tmp = str.split(",");
array[i] = tmp;
}
}
file.txt
W,W,W,W,W,W,W,W,W
B,B,B,B,B,B,B,B,B
C,C,C,C,C,C,C,C,C
D,D,D,D,D,D,D,D,D
To use that you would need to return the array from the function so you will need to do something like this.
var dir = 'file.txt';
var result; // Just to store the output
function file() {
const fileUrl = dir;
fetch(fileUrl)
.then(response => response.text())
.then(text => {
result = readFile(text)
})
}
function readFile(text) {
var lines = text.split('\n');
var array = [];
var str;
for (var i = 0; i < lines.length; i++) {
var str = lines[i];
tmp = str.split(",");
array[i] = tmp;
}
return array;
}
Notice the variable result on the second line.

How to assign an object from column values in Google Sheet?

I have a google sheet with some data in a single column.
How to assign these values to new object like {'value[0]':value[0], 'value[1]':value[1],..., 'value[i]':value[i]}?
I wrote this script, but it assigns pair from last value of names only:
function getIngredientsList() {
const url = "SPREADSHEET_URL";
const ss = SpreadsheetApp.openByUrl(url);
const ws = ss.getSheetByName('Base');
const names = ws.getRange(2, 3, ws.getLastRow() - 1).getValues().map(a => a[0]);
let nameList;
for (let i = 0; i < names.length; i++){
if (names[i] !== ""){
let name = {[names[i]]:names[i]};
nameList = Object.assign(name);
}
}
return nameList;
}
Where I'm wrong and how to fix it?
I believe your goal as follows.
You want to retrieve the values from the cells "C2:C28", and want to create an object that the key and value are the same.
For this, how about this modification? The arguments of Object.assign() is Object.assign(target, ...sources). So in your script, nameList of let nameList = {} is required to be used like nameList = Object.assign(nameList, name).
From:
let nameList;
for (let i = 0; i < names.length; i++){
if (names[i] !== ""){
let name = {[names[i]]:names[i]};
nameList = Object.assign(name);
}
}
To:
let nameList = {}; // Modified
for (let i = 0; i < names.length; i++){
if (names[i] !== ""){
let name = {[names[i]]:names[i]};
nameList = Object.assign(nameList, name); // Modified
}
}
Or, as other pattern, when reduce is used, the following script can also return the same value with above modified script.
const nameList = ws.getRange(2, 3, ws.getLastRow() - 1).getValues()
.reduce((o, [e]) => {
if (e != "") Object.assign(o, {[e]: e});
return o;
}, {});
Reference:
Object.assign()
More cleaner way to do that is using reduce method:
var names = ['ABC', 'XYZ', 'PQR'];
var result = names.reduce((acc, elem)=>{
acc[elem] = elem;
return acc;
},{});
console.log(result);

Dynamically assigning object keys results in last key being contained with double quotes

I've created a CSV parser and have run into an interesting situation. When the onLoad function is hit, the last key of my object row is double quoted. I'm looking for an explanation of why. Anyone know?
Comments contains sample values
const results = [];
const lines = reader.result.split('\n');
//lines = ["accountId,nickname,rando", "test-arn,test-nickname,test-rando"]
const keys = lines[0].split(',');
//keys = ["accountId", "nickname", "rando"]
forEach((line) => {
// line = "test-arn, test-nickname, test-rando"
const values = line.split(',');
// values = ["test-arn", "test-nickname", "test-rando"]
const row = {};
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
row[key] = values[i];
}
//row = { accountId: "test-arn", nickname="test-nickname", "rando": "test-rando"}
results.push(row);
}, lines);
You can see that the key rando inside row is surrounded by double quotes
row = {
accountId: "test-arn",
nickname: "test-nickname",
"rando": "test-rando",
}
Sample CSV
accountId,nickname,rando^M
test-arn,test-nickname,test-rando^M
Full Function
const parseCsv = ({ file, before, onSuccess, onError }) => {
before();
const reader = new FileReader();
reader.readAsText(file);
reader.onload = () => {
const results = [];
const lines = reader.result.split('\n');
const keys = lines[0].split(',');
forEach((line) => {
const values = line.split(',');
const row = {};
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
row[key] = values[i];
}
results.push(row);
}, lines);
onSuccess(results);
};
reader.onError = (error) => {
onError(error);
};
};
I've found that there is a return character being left in the last key.
In the console:
keys
>(3) ["accountId", "nickname", "rando"]
JSON.strigify(keys)
>"["accountId","nickname","rando\r"]"
Resolution is to remove the return character
const results = [];
const lines = reader.result.split('\n');
const header = lines.shift().replace(/(\r\n|\n|\r)/gm, ''); //Remove newline characters
const keys = header.split(',');
forEach((line) => {
const values = line.replace(/(\r\n|\n|\r)/gm, '').split(',');
const row = {};
for (let i = 0; i < keys.length; i++) {
row[keys[i]] = values[i];
}
results.push(row);
}, lines);
onSuccess(results);

how can I parse json with multiple lines

I have the following JSON:
[{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557704","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557705","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557706","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557707","mobile":"400089151"}]
I need to extract all "phoneNumber" using a js function.
I'm testing from using html and my function is not so good:
function getNumbers(strJSON)
{
strJSON = "[{\"errorMessage\":\"success\",\"mobile\":\"400089151\",\"phoneNumber\":\"400557704\",\"returnCode\":\"0\"},{\"errorMessage\":\"success\",\"mobile\":\"400089151\",\"phoneNumber\":\"400557705\",\"returnCode\":\"0\"},{\"errorMessage\":\"success\",\"mobile\":\"400089151\",\"phoneNumber\":\"400557706\",\"returnCode\":\"0\"}]";
var len = strJSON.length;
var begin_index = strJSON.indexOf("returnCode") - 2;
var last_index = len - 1;
var string_toSplit = strJSON.substring(begin_index, last_index);
var string_splitted = string_toSplit.split("{");
var out="";
alert(strJSON);
alert("string_splitted");
alert(string_splitted);
for ( var i = 0; i < string_splitted.length; i++)
{
if (string_splitted[i].charAt(string_splitted[i].length - 1) === ",")
{
string_splitted[i] = string_splitted[i].slice(0, -1);
}
var json = "{" + string_splitted[i];
var obj = JSON.parse(json);
if (i == string_splitted.length)
{
out = out + obj.phoneNumber;
}
else
{
out = out + obj.phoneNumber + ",";
}
}
return out;
}
For modern browsers you can use the .map() method
var j = [{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557704","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557705","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557706","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557707","mobile":"400089151"}];
var phones = j.map(function(item){return item.phoneNumber});
Update
After seeing your code (do not try to manually split/parse the json string.. use the JSON.parse method) you should use
function getNumbers(strJSON)
{
var myJson = JSON.parse( strJSON );
return myJson.map(function( item ){ return item.phoneNumber}).join(',');
}
Update: An even better way:
function getNumbers(strJSON)
{
var obj = JSON.parse(strJSON);
return obj.map(x => x.phoneNumber).join(", ")
}
Original Post:
A straight forward method is to just iterate over every object in the array and take the values out individually.
var info = [{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557704","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557705","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557706","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557707","mobile":"400089151"}];
var phoneNumbers = [];
for (var i = 0; i < info.length; i++)
{
phoneNumbers.push(info[i].phoneNumber);
}
console.log(phoneNumbers);
http://jsfiddle.net/hX69r/
UPDATE:
http://jsfiddle.net/hX69r/1/
var info = [{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557704","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557705","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557706","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557707","mobile":"400089151"}];
var infoString = JSON.stringify(info); //this just turns the object array 'info' into a string
var numbers = getNumbers(infoString);
console.log(numbers);
function getNumbers(strJSON)
{
var obj = JSON.parse(strJSON);
var phoneNumbers = [];
for (var i = 0; i < obj.length; i++)
{
phoneNumbers.push(obj[i].phoneNumber);
}
return phoneNumbers.join(", ");
}
Additional Update:
var info = [{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557704","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557705","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557706","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557707","mobile":"400089151"}];
var infoSingle = {"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557704","mobile":"400089151"};
console.log(info.length); // prints 4; so you know it has the []
console.log(infoSingle.length); // prints undefined; so you know it doesn't have []
Do not try to re-invent the wheel.
There are many ways to parse JSON already:
Use JSON.parse.
Use jQuery.parseJSON

Categories