Reading CSV in D3 and converting it to array of arrays - javascript

I have the following small dataset hardcoded into a variable in my code:
var dataset = [['CENTRAL', 44, 552, 18565],
['NORTHERN', 42, 945, 20092],
['SOUTHERN', 96, 795, 30095],
['PARK', 1, 640, 9341],
['MISSION', 66, 1198, 18542],
['TENDERLOIN', 23, 113, 10735],
['RICHMOND', 9, 561, 9082],
['TARAVAL', 81, 789, 11966],
['INGLESIDE', 5, 1368, 13414],
['BAYVIEW', 7, 985, 14711]];
Now, this dataset is taken directly from a .csv file, that looks like this:
District,Prostitution,VehicleTheft,Totalcrimecount
CENTRAL,44,552,18565
NORTHERN,42,945,20092
SOUTHERN,96,795,30095
PARK,1,640,9341
MISSION,66,1198,18542
TENDERLOIN,23,113,10735
RICHMOND,9,561,9082
TARAVAL,81,789,11966
INGLESIDE,5,1368,13414
BAYVIEW,7,985,14711
However, I'd obviously like to be able to just read in the file, which I've attempted using this:
var dataset_csv = d3.csv("datafile.csv", function(d){
console.log(dataset_csv = d)
});
Which gives me an array of objects that look like this:
[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
District: "CENTRAL"
Prostitution: "44"
Totalcrimecount: "18565"
VehicleTheft: "552"
My question is, and it might be trivial, how can I transform those objects into an array data structure like my initial dataset? I've been battling with it for some time now. Any hints greatly appreciated.

Use Array#map to iterate over each object and Object.keys to catch only the keys values.
var dataset_csv = [{District: "CENTRAL", Prostitution: "44", Totalcrimecount: "18565", VehicleTheft: "552"}, {District: "WEST", Prostitution: "22", Totalcrimecount: "4324", VehicleTheft: "53"}, {District: "EAST", Prostitution: "11", Totalcrimecount: "23434" , VehicleTheft: "76"}],
datasetArr = dataset_csv.map(v => Object.keys(v).map(c => Number(v[c]) ? Number(v[c]) : v[c]));
console.log(datasetArr);

Related

How to convert a js object into an array?

My data:
{product_quantity: {quantity: 13, code: "AAA", warehouse: "1000",}}
product_quantity is an field from a mongodb json data.
I need to convert it into this:
{"product_quantity": [{"quantity": 13, "code": "AAA", "warehouse": "1000"}]
How can I transform an object into array with its keys and values?
Thanks.
json you want is not valid , you can use this
let productQuantity={product_quantity:{quantity:13, code:"AAA", warehouse:"1000"}};
productQuantity.product_quantity=[productQuantity.product_quantity];
console.log(JSON.stringify(productQuantity));
result
{"product_quantity":[{"quantity":13,"code":"AAA","warehouse":"1000"}]}
const data = [quantity];
output;
[{quantity: 13, code: "AAA", warehouse: "1000",}];

Nodejs Multiple map arrays to single Json file

I'm new to nodejs and am trying to write a Json parser that will take data from a Json API, allow me to grab the data I want (some of this data I will transform) and then write to a Json file.
I have discovered the map command which is extracting the raw data and this is giving me an array for example
data['home_team'] = json['data'].map(smdata => smdata.localTeam.data.name);
data['awayid'] = json['data'].map(smdata => smdata.visitorTeam.data.id);
data['away_team'] = json['data'].map(smdata => smdata.visitorTeam.data.name);
And this works perfectly, now I'm left with an array which I want to turn into a Json file, I can of course just run a FOR loop and iterate through each of the entries and write to a file but I wondered if there was a more efficent way to do this
Thanks
Take a look at JSON.stringify to turn your data into a string you can write to a file.
In your comments below, you provide an invalid JS structure, an array with keys - maybe a typo with square braces (array) rather than curly braces (object). Or maybe note that Array map returns an array, so you may wish to look at Array.reduce, depending on your data.
let data = {
islive: [90, 90, null, null],
ls: [1, 1, 1, 1],
time: [90, 90, null, null],
status: ['FT', 'FT', 'NS', 'NS'],
starttime: [1616036700, 1616050800, 1616058000, 1616058900],
matchid: [17706353, 17926675, 17869155, 17926676]
};
// Dump it as JSON:
console.log(JSON.stringify(data));
// Pretty print with 2 space indent:
console.log(JSON.stringify(data, {}, 2));
// Write it to file 'my.json':
const fs = require('fs');
fs.writeFileSync(
'my.json',
JSON.stringify(data)
);
Outputs:
{"islive":[90,90,null,null],"ls":[1,1,1,1],"time":[90,90,null,null],"status":["FT","FT","NS","NS"],"starttime":[1616036700,1616050800,1616058000,1616058900],"matchid":[17706353,17926675,17869155,17926676]}
{
"islive": [
90,
90,
null,
null
],
"ls": [
1,
1,
1,
1
],
"time": [
90,
90,
null,
null
],
"status": [
"FT",
"FT",
"NS",
"NS"
],
"starttime": [
1616036700,
1616050800,
1616058000,
1616058900
],
"matchid": [
17706353,
17926675,
17869155,
17926676
]
}

JSON to object array format

I want to display like convert JSON data to array objects how can solve this problem using jquery or js?
JSON data :
[{
"new": 122,
"old": 3389,
"boarding": 1,
"aws": 10,
"azure": 12,
"cli": 41
}];
To array object:
[
["new", 122],
["old", 3389],
["boarding", 1]
]
one more pattern I need it I have an array like this in Ruby
[37,
3,
261,
1,
0,
1457]
to convert into add as key entry static
[
["new",37],
["old",3],
["boarding",1]
["aws",0]
["azure",1457]
]
Firstly note that you do not need jQuery at all for this. jQuery is primarily a tool for amending the DOM. To work with data structures like this native JS is all you need.
To achieve what you require, you can use Object.keys() to get the keys of the 0th object in your original array, then you can loop over them using map() to build a new 2 dimensional array from that data:
var originalArr = [{
"new": 122,
"old": 3389,
"boarding": 1,
"aws": 10,
"azure": 12,
"cli": 41
}];
var newArr = Object.keys(originalArr[0]).map(function(key) {
return [key, originalArr[0][key]];
});
console.log(newArr);
Its simple enough. Just enter the array into Object.entries(). like this Object.entries(originalArr[0])
A simple one-line alternative:
const data = [{
"new": 122,
"old": 3389,
"boarding": 1,
"aws": 10,
"azure": 12,
"cli": 41
}];
const result = Object.keys(data[0]).map(k => ([k, data[0][k]]));
console.log(result);

JSON understanding and arrays

I am in process of reverse engineering a JS script. Somewhere is says:
var a = [{
name: 'sample1',
data: ["Otu1", "Otu2", "Otu3", "Otu4", "Otu5"],
values: [5, 15, 250, 20, 23]
},{
name: 'sample2',
data: ["Otu1", "Otu5", "Otu6", "Otu7"],
values: [234, 29, 239, 5]
}]
First question: What type of object is it? is it JSON? Or is it an array of JSON objects?
I need to write this in this form:
var b = {
name: 'sample1',
data: ["Otu1", "Otu2", "Otu3", "Otu4", "Otu5"],
values: [5, 15, 250, 20, 23]
}
var c = {
name: 'sample2',
data: ["Otu1", "Otu5", "Otu6", "Otu7"],
values: [234, 29, 239, 5]
}
var a = b + c
Could you please help? Any insights are appreciated. Thank you community !
"First question: What type of object is it? is it JSON? Or is it an array of JSON objects?"
It's an Array of JavaScript Objects. It could be serialized into JSON data, but currently you should just see it as JavaScript code. The notation is similar, but the resulting data is different.
(And actually in your case, for the notation to be JSON-like, you'd need to use double quotes. But even then, you're still creating JavaScript Objects)
"I need to write this in this form: "
For this, you could make an Array of JavaScript Objects like this:
var a = [b, c];
You have an array of Objects here, remember JSON simply means JavaScript Object Notation

Split a string array into a number array in javascript

I am reading a value from a dropdown list depending on what option is selected. I am using jqPlot to graph the values.
jqPlot expects an array of values like [91, 6, 2, 57, 29, 40, 95]
but when I read the value in from the dropdown box it is coming in as a whole string "[91, 6, 2, 57, 29, 40, 95]"
I tried splitting it but I got ["91", "6", "2", "57", "29", "40", "95"] which wont display the graph correctly.
Is there anybody that has encountered something like this before and what can I do to make my values convert into a number array.
Thanks for any help
You can use JSON.parse() to convert that string into a JavaScript array. The numbers in the string are not quoted so the array will also contain numbers. And you can delete all the code that parses the string as you won't need it anymore.
>>> JSON.parse("[91, 6, 2, 57, 29, 40, 95]")
[91, 6, 2, 57, 29, 40, 95]
If you need to support legacy browsers, add json2.js to shim JSON support in browsers not supporting it natively.
You can use JSON.parse to change the string "[91, 6, 2, 57, 29, 40, 95]" into an array:
​(function(){
var a = '[1,2,3]';
var b = JSON.parse('[1,2,3]');
alert(b +'\n'+ typeof(b)+'\n'+ b[0] );
})()​
Demo
As you are using jQuery you can do
$.parseJSON("[91, 6, 2, 57, 29, 40, 95]");
you can use
str.slice(1, -1).split(',').map(function(s){return parseInt(s, 10);});

Categories