d3 - return only data without header - javascript

I have a csv data.
DATA :
Time,Count
1377973800,293
1377975600,212
1377977400,129
1377979200,89
1377981000,54
1377982800,21
1377984600,15
I want to return the data in this format.
{
"946705035":4,
"946706692":4,
"946707210":0,
"946709243":2,
"946710714":5,
"946712907":3,
"946713183":4,
"946719001":0
}
I do not want the header Time and Count to be appeared in the json format.
Tried using d3.nest() but the result I got like it starts with key variable which i don't want.
Someone please help me in getting the data in that format.

I believe a code similar to this would do the job:
d3.csv("data.csv", function(error, data) {
var myObject = {};
for (var i=0; i < data.length; i++)
myObject[data[i].Time] = data[i].Count;
};
This gives you data about counts as strings, and if you want numbers, you can just add a "+", which will trigger conversion from string to number:
myObject[data[i].Time] = +data[i].Count;
EDIT: Here is related question on creating object properties dynamically, maybe you can find something useful there too.

Related

Function returning object instead of Array, unable to .Map

I'm parsing an order feed to identify duplicate items bought and group them with a quantity for upload. However, when I try to map the resulting array, it's showing [object Object], which makes me think something's converting the return into an object rather than an array.
The function is as follows:
function compressedOrder (original) {
var compressed = [];
// make a copy of the input array
// first loop goes over every element
for (var i = 0; i < original.length; i++) {
var myCount = 1;
var a = new Object();
// loop over every element in the copy and see if it's the same
for (var w = i+1; w < original.length; w++) {
if (original[w] && original[i]) {
if (original[i].sku == original[w].sku) {
// increase amount of times duplicate is found
myCount++;
delete original[w];
}
}
}
if (original[i]) {
a.sku = original[i].sku;
a.price = original[i].price;
a.qtty = myCount;
compressed.push(a);
}
}
return compressed;
}
And the JS code calling that function is:
contents: compressedOrder(item.lineItems).map(indiv => ({
"id": indiv.sku,
"price": indiv.price,
"quantity": indiv.qtty
}))
The result is:
contents: [ [Object], [Object], [Object], [Object] ]
When I JSON.stringify() the output, I can see that it's pulling the correct info from the function, but I can't figure out how to get the calling function to pull it as an array that can then be mapped rather than as an object.
The correct output, which sits within a much larger feed that gets uploaded, should look like this:
contents:
[{"id":"sku1","price":17.50,"quantity":2},{"id":"sku2","price":27.30,"quantity":3}]
{It's probably something dead simple and obvious, but I've been breaking my head over this (much larger) programme till 4am this morning, so my head's probably not in the right place}
Turns out the code was correct all along, but I was running into a limitation of the console itself. I was able to verify this by simply working with the hard-coded values, and then querying the nested array separately.
Thanks anyway for your help and input everyone.
contents: compressedOrder(item.lineItems).map(indiv => ({
"id": indiv.sku,
"price": indiv.price,
"quantity": indiv.qtty
}))
In the code above the compressedOrder fucntion returns an array of objects where each object has sku, price and qtty attribute.
Further you are using a map on this array and returning an object again which has attributes id, price and quantity.
What do you expect from this.
Not sure what exactly solution you need but I've read your question and the comments, It looks like you need array of arrays as response.
So If I've understood your requirement correctly and you could use lodash then following piece of code might help you:
const _ = require('lodash');
const resp = [{key1:"value1"}, {key2:"value2"}].map(t => _.pairs(t));
console.log(resp);
P.S. It is assumed that compressedOrder response looks like array of objects.

A loop but selective numbers

I have to retrieve data from an API.
Certain data has to be retrieved in a certain order.
To be exact, data needs to be retrieved in this order:
7,40,8,9,10,45,11,39,5,12,13,15,6,18,0,46,22,23,3,41,1,24,42,25,26,4,27,2
So when loop is doing 0, then it needs to retrieve data number 7, when loop is doing 1, then data number 40 and if loop is doing 2 then data number 8 etc.
listWithDataFromAPI I do this:
metricsSheet.appendRow([listWithDataFromAPI.symbols]);
And get this response:
[Ljava.lang.Object;#1e1aaea2
When I insert a specific number I do this:
metricsSheet.appendRow([listWithDataFromAPI.symbols].symbols[8]]);
And get such response: {name=DataPeter, longVolume=6640.87, longPositions=23678}
Thus if loop is 0 then extract 7, loop is 2 extract 40 etc. as I mentioned.
This is what I'm trying in concept:
var listNumberValue = ["5","30","7"];
var symbols = listWithDataFromAPI.symbols;
for (var i in listNumberValue) {
var symbol = symbols[listNumberValue];
metricsSheet.appendRow([symbol.name]);
}
Hope this makes sense.
Not sure how to do this..?
The big problem for us is that we don't know what listWithDataFromAPI is and you have not explained it. But I think you're trying to say that you want to iterate over something that you can get from it so I took a stab at what I think you're trying to do.
The first function will take your 0 through 46 indexes and reorder as 7,40,8,9,10,45,11,39,5,12,13,15,6,18,0,46,22,23,3,41,1,24,42,25,26,4,27,2
As shown in this table.
function getIdxObj() {
var idxA=[7,40,8,9,10,45,11,39,5,12,13,15,6,18,0,46,22,23,3,41,1,24,42,25,26,4,27,2];
var idxObj={};
for(var i=0;i<idxA.length;i++) {
idxObj[i]=idxA[i];
}
return idxObj;
}
The second function loops over symbols which apparently come from listWithDataFromAPI.symbols which has not been explained and so we know nothing about it.
function theUnknownFunction() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('metrics');//not sure about this either
var idxObj=getIdxObj();
var symbols = listWithDataFromAPI.symbols; //I dont have a clue as to what listWithDataFromAPI is
for (var i=0;i<symbols.length;i++) {
var symbol = symbols[idxObj[i]];
sh.appendRow([symbol.name]);
}
}

Parsing JSON and loading into array

JSON-https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo
I am trying to take the JSON from the above link and place it in the following format(date,open,high,low,close)...
[
[1277424000000,38.58,38.61,37.97,38.10],
[1277683200000,38.13,38.54,37.79,38.33],
[1277769600000,37.73,37.77,36.33,36.60],
[1277856000000,36.67,36.85,35.72,35.93],
]
The date does not need to be Epoch time.
My code....
$.getJSON('https://www.alphavantage.co/query?
function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo', function(data) {
//Get the time series data
var timeseries = data['Time Series (Daily)']
var ohlcarray = [];
//Loop through each time series and convert it to JSON format
$.each(timeseries, function(key, value) {
var ohlcdata=[];
ohlcdata[0]=value[0];//date
ohlcdata[1]=value[1];//open
ohlcdata[2]=value[2];//high
ohlcdata[3]=value[3];//low
ohlcdata[4]=value[4];//low
ohlcarray.push(ohlcdata);
});
console.log(ohlcarray[0]);//test if worked properly
});
The output....
[undefined, undefined, undefined, undefined, undefined]
value[x] returns undefined. Any ideas on why this happens?
Thanks!
The reason is that in the each loop, the value is not a list but an object. To access it, you'd have to grab it using keys.
ohlcdata[0] = key; //date
ohlcdata[1] = value['1. open']; //open
ohlcdata[2] = value['2. high']; //high
ohlcdata[3] = value['3. low']; //low
ohlcdata[4] = value['4. close']; //low
https://jsfiddle.net/koralarts/7c2fkf93/2/
At this point it is already JSON. As Patrick mentioned, getJSON automatically parses json. You will need to access it with the array bracket notation since there are spaces in the property name.
var timeSeries = data['Time Series (Daily)'];
See ex: https://jsfiddle.net/dz0phhn2/6/

convert or parse string to JSON object

There was an interview question(Javascript) which my friend and me unable to solve it for a long time, so thought of asking here,
Question:
String:
2014<18.3,11.4,12.1,19.5,1000&&11.2,34.5,67.1,18,20000>name=sample,position=engineer,company=abc
and the end reult should be a JSON Objwct with following format. Can anyone please help to solve this issue.
Output:
{[
{"Proposal":"2014"},
{"values":"[18.3,11.4,12.1,19.5],[11.2,34.5,67.1,18]"},
{"Items":"[1000,20000]"},
{"name":"sample"},
{"position":"engineer"},
{"company":"abc"},
]}
Expecting a solution and explanation please.
Thanks,
Basky
Here is your solution.
Input string should have all the parameter, if any of the parameter is missing below code will break.
Logic is dynamic to support any number of numeric values where as last digit will consider as Items.
Logic also support the n number of key/value pair in the input string.
Check below code in console.
var JsonOutput = [];
var sRawInput = "2014<18.3,11.4,12.1,19.5,1000&&11.2,34.5,67.1,18,20000>name=sample,position=engineer,company=abc";
JsonOutput.push({ "Proposal" : sRawInput.split("<")[0] });
var oValues = sRawInput.split("<")[1].split(">")[0].split("&&");
var oActualValues = [];
var oActualItems = [];
$(oValues).each(function(Ind, Val){
oActualValues.push(Val.split(",").slice(0, Val.split(",").length - 1).join());
oActualItems.push(Val.split(",")[Val.split(",").length - 1]);
});
JsonOutput.push({ "Values" : oActualValues });
JsonOutput.push({ "Items" : oActualItems });
var OtherValues = sRawInput.split(">")[1].split(",");
$(OtherValues).each(function(Ind, Val){
JsonOutput.push(JSON.parse("{\"" + Val.split("=")[0] + "\":\"" + Val.split("=")[1] + "\"}"));
});
console.log(JsonOutput);
console.log(JSON.stringify(JsonOutput));
Note:
Values is incorrect as it should contain 2 arrays and not a single array.
Items is incorrect as it should contain numbers and not a string.
Also the outside wrapper should be an obj {}

d3: skipping lines when reading in csv file

I'm trying to read in a csv file with D3 and I'm a little stuck. The way my csv file is formatted is that the first line is a merged cell containing a year then the next line will contain the data descriptions (name, age etc).
Currently I have the following:
var resourceList = [{description: "All Yearly Data",
name: "yearlyData",
path: "data.csv"};
d3.csv(resourceInfo.path, function(error, d) {
theData.resources[resourceInfo.name].processed = true;
theData.resources[resourceInfo.name].error = error;
theData.resources[resourceInfo.name].data = d;
theData.numProcessed += 1;
});
This reads the first line in as the data descriptions and then the following lines as actual data. What I want to do is have an multidimensional array which I could go through by year. Is it possible to skip lines while parsing to make sure I can manage that or no?
Thanks!
one way of getting at this would be to use filter:
d3.csv(resourceInfo.path, function(error, d) {
var newData = d.filter(function(obs) { return INSERT YOUR FILTER CONDITION HERE;});
...
see also:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FArray%2Ffilter

Categories

Resources