Javascript: divide object date value per day - javascript

Hi Everyone,
I have this object:
{"2017-07-09 00:00:00":4,"2017-07-09 09:00:00":1,"2017-07-09 10:00:00":4,"2017-07-09 11:00:00":3,"2017-07-09 12:00:00":16,"2017-07-09 13:00:00":4,"2017-07-09 14:00:00":6,"2017-07-09 15:00:00":5,"2017-07-09 16:00:00":7,"2017-07-09 17:00:00":21,"2017-07-09 18:00:00":25,"2017-07-09 19:00:00":1,"2017-07-10 09:00:00":11,"2017-07-10 10:00:00":4,"2017-07-10 11:00:00":21,"2017-07-10 12:00:00":22,"2017-07-10 13:00:00":23,"2017-07-10 14:00:00":42,"2017-07-10 15:00:00":14,"2017-07-10 16:00:00":36,"2017-07-10 17:00:00":21,"2017-07-10 18:00:00":5,"2017-07-11 09:00:00":16,"2017-07-11 10:00:00":7,"2017-07-11 11:00:00":26,"2017-07-11 12:00:00":34,"2017-07-11 13:00:00":39,"2017-07-11 14:00:00":39,"2017-07-11 15:00:00":30,"2017-07-11 16:00:00":33,"2017-07-11 17:00:00":22,"2017-07-11 18:00:00":1}
I'm trying to divide it per day and get something like this:
First create an array for the day which is 24 hours:
[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
And than calculate how many day is in the object above and get a value for each hours and return something like below. In the object I have 3 days, so I need to return 3 arrays for each days:
[4,0,0,0,0,0,0,0,0,1,10,3,16,...],
[0,0,0,0,0,0,0,0,0,11,4,21,22,23,42,...]
[0,0,0,0,0,0,0,0,0,16,7,26,34,39,39,30,..]
For each hours which is not in the object data return 0 and for each hours that exist return the specified value on the object.
for example on the object the time starts from "2017-07-09 00:00:00":4 and it has the value of 4 and for each hours between that and "2017-07-09 09:00:00":1
return 0.
Any help appreciated. Thanks a lot for your time

You have first to loop into each keys of your object.
After that many solutions, one is to convert your keys into Date object and compare the hour of your object in a for loop.
And retrieve the right data by reconverting your Date obj into your obj property.
Edit
Access your property value without function by using Object.keys and findIndex
I've maybe misunderstand your requirement, but you've a good example
of how to do ;-)
Example of solution
var data ={"2017-07-09 00:00:00":4,"2017-07-09 09:00:00":1,"2017-07-09 10:00:00":4,"2017-07-09 11:00:00":3,"2017-07-09 12:00:00":16,"2017-07-09 13:00:00":4,"2017-07-09 14:00:00":6,"2017-07-09 15:00:00":5,"2017-07-09 16:00:00":7,"2017-07-09 17:00:00":21,"2017-07-09 18:00:00":25,"2017-07-09 19:00:00":1,"2017-07-10 09:00:00":11,"2017-07-10 10:00:00":4,"2017-07-10 11:00:00":21,"2017-07-10 12:00:00":22,"2017-07-10 13:00:00":23,"2017-07-10 14:00:00":42,"2017-07-10 15:00:00":14,"2017-07-10 16:00:00":36,"2017-07-10 17:00:00":21,"2017-07-10 18:00:00":5,"2017-07-11 09:00:00":16,"2017-07-11 10:00:00":7,"2017-07-11 11:00:00":26,"2017-07-11 12:00:00":34,"2017-07-11 13:00:00":39,"2017-07-11 14:00:00":39,"2017-07-11 15:00:00":30,"2017-07-11 16:00:00":33,"2017-07-11 17:00:00":22,"2017-07-11 18:00:00":1}
var dataKeys = Object.keys(data);
var dataKeysParsed = dataKeys.map(function(d){
return new Date(d);
});
var res =[];
for (var i=0;i<=23;i++){
var id = dataKeysParsed.findIndex(function(k){
return k.getHours() == i
});
res.push(id > -1 ? data[dataKeys[id]] : 0);
}
console.log(res);

Related

Set the first 10 value of the typedArray to 1000

Here I'm using a TypedArray Uint32Array. Where I have some set of data in the data. I want to replace the first 10 data of the array with some value. I'm trying to set it to the first 10 value but it is not binding respective to the first 10 data of the array. Kindly, help me out. Thanks in advance.
var arrD = new Uint32Array(32)
arrD.slice(0, 12).forEach(function (item) {
arrD[item] = 5
console.log(arrD)
})
You could use the .fill array function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill
var arrD = new Uint32Array(32);
arrD.fill(5, 0, 10) // fills array with value from index 0 - 9

Sort Nested Json Object based on value in for loop?

Hoping someone can help me out here or at least point me in the right direction. I have spent hours trying to get this sorted and I am lost.
The code below is just mock, my actual json is returned via AJAX using jquery. My problem is not sorting, but sorting on a nested json object.
I am trying to sort the json output based on cost. (lowest cost to highest), my attempts have failed and I cannot get this sorted. I keep getting "sort" is undefined.
Any help would be appreciated or if you can just point out what I am doing wrong here.
var json = '{"shipping_method":{"ups":{"title":"United Parcel Service","quote":{"12":{"code":"ups.12","title":"UPS 3 Day Select","cost":117.3,"tax_class_id":"0","text":"$117.30"},"13":{"code":"ups.13","title":"UPS Next Day Air Saver","cost":242.52,"tax_class_id":"0","text":"$242.52"},"14":{"code":"ups.14","title":"UPS Next Day Air Early A.M.","cost":279.95,"tax_class_id":"0","text":"$279.95"},"03":{"code":"ups.03","title":"UPS Ground","cost":54.62,"tax_class_id":"0","text":"$54.62"},"02":{"code":"ups.02","title":"UPS 2nd Day Air","cost":177.31,"tax_class_id":"0","text":"$177.31"},"01":{"code":"ups.01","title":"UPS Next Day Air","cost":248.08,"tax_class_id":"0","text":"$248.08"}},"sort_order":"","error":""}}}';
/*
This doesnt work and returns undefined.
json["shipping_method"]["quote"].sort(function(a, b) {
return a['cost'] > b['cost'];
});
// I found this example, but also didn't work.
custSort = (prop1, prop2 = null, direction = 'asc') => (e1, e2) => {
const a = prop2 ? e1[prop1][prop2] : e1[prop1],
b = prop2 ? e2[prop1][prop2] : e2[prop1],
sortOrder = direction === "asc" ? 1 : -1
return (a < b) ? -sortOrder : (a > b) ? //sortOrder : 0;
};
json.sort(custSort("quote", "cost", "desc"));*/
json = JSON.parse(json);
for (var i in json["shipping_method"]) {
// EDIT:: I want the sorting to occur here if possible.
for (j in json["shipping_method"][i]["quote"]) {
//EDIT:: I want to keep this for loop, but with the results sorted by cost
console.log(json["shipping_method"][i]["quote"][j]["cost"]);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Convert Object to Sorted Array
The object can be flattened out so that parent keys are included in the deep object as it's been iterated over. An early example to that can be found in this answers edit history. It has been removed since information like the quote-id was not deemed important.
Below is an example of using Object.values, which traverses an object and only returns an array of that objects values (discarding the keys). The values can then be sorted as intended, by cost.
const json = JSON.parse(getData());
for (let method in json["shipping_method"]) {
// cache
let quotes = json['shipping_method'][method]['quote']
// convert object to array and sort
let sortedQuotes = Object.values(quotes).sort((a, b)=>a.cost-b.cost);
console.log(sortedQuotes)
}
/* Dummy Data */
function getData() {
return '{"shipping_method":{"ups":{"title":"United Parcel Service","quote":{"12":{"code":"ups.12","title":"UPS 3 Day Select","cost":117.3,"tax_class_id":"0","text":"$117.30"},"13":{"code":"ups.13","title":"UPS Next Day Air Saver","cost":242.52,"tax_class_id":"0","text":"$242.52"},"14":{"code":"ups.14","title":"UPS Next Day Air Early A.M.","cost":279.95,"tax_class_id":"0","text":"$279.95"},"03":{"code":"ups.03","title":"UPS Ground","cost":54.62,"tax_class_id":"0","text":"$54.62"},"02":{"code":"ups.02","title":"UPS 2nd Day Air","cost":177.31,"tax_class_id":"0","text":"$177.31"},"01":{"code":"ups.01","title":"UPS Next Day Air","cost":248.08,"tax_class_id":"0","text":"$248.08"}},"sort_order":"","error":""}}}';
}
.as-console-wrapper {
max-height: 100vh !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Quotes by Cost per Shipping Method
This assumes that the quote ID is needed (perhaps to be placed on a row); otherwise this can be simplified using Object.values in place of Object.entries (amongst other changes).
Disregard what the output function is doing. It is a quick example, that doesn't ensure proper cell order and has a host of other limitations and vulnerabilities. It is only used to demonstrate that the original quote data is still available after sorting.
const data = JSON.parse(getData());
for (let method in data.shipping_method) {
output({row: method}, {class:'capitalize'})
// cache
let quotes = data.shipping_method[method].quote
let sortContent = Object.entries(quotes);
let sortedQuotes = sortContent.sort((a,b)=>a[1].cost-b[1].cost).map(i=>i[0]);
for (let quoteId of sortedQuotes){
let quoteInfo = quotes[quoteId];
output({cell: quoteInfo})
}
}
/* Dummy Data */
function getData() {
return '{"shipping_method":{"ups":{"title":"United Parcel Service","quote":{"12":{"code":"ups.12","title":"UPS 3 Day Select","cost":117.3,"tax_class_id":"0","text":"$117.30"},"13":{"code":"ups.13","title":"UPS Next Day Air Saver","cost":242.52,"tax_class_id":"0","text":"$242.52"},"14":{"code":"ups.14","title":"UPS Next Day Air Early A.M.","cost":279.95,"tax_class_id":"0","text":"$279.95"},"03":{"code":"ups.03","title":"UPS Ground","cost":54.62,"tax_class_id":"0","text":"$54.62"},"02":{"code":"ups.02","title":"UPS 2nd Day Air","cost":177.31,"tax_class_id":"0","text":"$177.31"},"01":{"code":"ups.01","title":"UPS Next Day Air","cost":248.08,"tax_class_id":"0","text":"$248.08"}},"sort_order":"","error":""}}}';
}
/* Really simple output for demo purpose */
function output(data, options={}){
if ('row' in data){
let $col = $('<td></td>', options).html(data.row)
let $row = $('<tr></tr>').append($col);
$('tbody').append( $row )
}
else if ('cell' in data){
let $row = $('<tr></tr>')
for( let key in data.cell ){
let $col = $('<td></td>', options).html(data.cell[key])
$row.append($col)
}
$('tbody').append( $row )
}
}
.capitalize {
text-transform: uppercase;
}
td {
min-width: 5rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<thead></thead>
<tbody></tbody>
</table>
Your objects can have any amount of properties and you can choose to sort by whatever object property you want, number or string, if you put the objects in an array.
var item = JSON.parse(json).shipping_method.ups.quote;
Use Object.values() to get array of values of the JSON object and then use slice() method to copy the array of JSON objects and not just make a reference.
var byCost = Object.values(item).slice(0);
Finally you can use sort function for that array of objects.
byCost.sort(function(a,b) {return a.cost - b.cost});
var json = '{"shipping_method":{"ups":{"title":"United Parcel Service","quote":{"12":{"code":"ups.12","title":"UPS 3 Day Select","cost":117.3,"tax_class_id":"0","text":"$117.30"},"13":{"code":"ups.13","title":"UPS Next Day Air Saver","cost":242.52,"tax_class_id":"0","text":"$242.52"},"14":{"code":"ups.14","title":"UPS Next Day Air Early A.M.","cost":279.95,"tax_class_id":"0","text":"$279.95"},"03":{"code":"ups.03","title":"UPS Ground","cost":54.62,"tax_class_id":"0","text":"$54.62"},"02":{"code":"ups.02","title":"UPS 2nd Day Air","cost":177.31,"tax_class_id":"0","text":"$177.31"},"01":{"code":"ups.01","title":"UPS Next Day Air","cost":248.08,"tax_class_id":"0","text":"$248.08"}},"sort_order":"","error":""}}}';
var item = JSON.parse(json).shipping_method.ups.quote;
var byCost = Object.values(item).slice(0);
byCost.sort(function(a,b) {return a.cost - b.cost});
console.log(byCost)
It doesn't seem like you are accessing the right path here... Looking at the JSON you posted you should be attempting to sort shipping_method.ups.quote its also worth noting that shipping_method.ups.quote is an object and must be converted to an Array to invoke .sort as this method lives on the Array prototype.
This can be done several ways but Object.values() is one such way.
you may try the following,
json = JSON.parse(json);
let item = json.shipping_method.ups.quote,
temp = [];
for (let key in item) {
temp.push(item[key]);
}
temp.sort((x, y) => x.cost - y.cost);
json.shipping_method.ups.quote = temp;
converting your object into array and then sort;
As i can see , your problem is to sort the object with cost as their keys should stays same ,
try out this ,
var json = '{"shipping_method":{"ups":{"title":"United Parcel Service","quote":{"12":{"code":"ups.12","title":"UPS 3 Day Select","cost":117.3,"tax_class_id":"0","text":"$117.30"},"13":{"code":"ups.13","title":"UPS Next Day Air Saver","cost":242.52,"tax_class_id":"0","text":"$242.52"},"14":{"code":"ups.14","title":"UPS Next Day Air Early A.M.","cost":279.95,"tax_class_id":"0","text":"$279.95"},"03":{"code":"ups.03","title":"UPS Ground","cost":54.62,"tax_class_id":"0","text":"$54.62"},"02":{"code":"ups.02","title":"UPS 2nd Day Air","cost":177.31,"tax_class_id":"0","text":"$177.31"},"01":{"code":"ups.01","title":"UPS Next Day Air","cost":248.08,"tax_class_id":"0","text":"$248.08"}},"sort_order":"","error":""}}}';
var json = JSON.parse(json);
let data = []
for(var i in json.shipping_method.ups.quote){
data.push(json.shipping_method.ups.quote[i])
data.sort((a,b) => a.cost - b.cost);
}
This create those key agains as they are before
let final = {} ;
data.forEach(el => final[el.code.split('.')[1]] = el);
Finally update the qoute with the latest sorted quotes :
json.shipping_method.ups.quote = final;

JavaScript return object property value

I have this object:
{"Header":["Date","Test1","Test2","Test3","N/A","Test4","Test5"],
"Values":[["Total Unique","79 280","1 598","5 972","20","2 633","9 696"],
["2017-06-19","28 026","1 036","3 667","20","1 097","4 672"]]}
My desired result is this:
Date
2017-06-19
What I was able to achieve:
Date ["2017-06-19","28 026","1 036","3 667","20","1 097","4 672"]
Using this code:
vm.header = data.Header[0];
vm.data1 = data.Values[1];
Because data.Values is a two-dimensional array you can get the desired result by changing the code to:
vm.header = data.Header[0];
vm.data1 = data.Values[1][0];
Header[0] = 'Date';
Header[1]= 'Test1';
Header[2]= 'Test2';
Header[3]= 'Test3';
Header[4]= 'N/A';
Header[5]= 'Test4';
Header[6]= 'Test5';
Values is 2D array
Values[0] = ["Total Unique","79 280","1 598","5 972","20","2 633","9 696"]
Values[1]=["2017-06-19","28 026","1 036","3 667","20","1 097","4 672"]
What you have tried so far is data.Header[0] would give you 'Date'. data.Values[1] would give you whole array. So you need to get "2017-06-19" you have to get first element of that array ie data.Values[1][0]

Merge two object arrays for Chart

I've two arrays which I'm going to merge and then load in chart.
range = date array generated from moment.js, there is date of every day in example one month or specific date range with one another attribute count: "0"
data = fetched data from database through backbone
Now I want set atrribute count from data count, where date is same in both arrays.
I'm using lodash...
_.forEach(range, function(n) {
console.log(n.date.substring(0, date.length-6));
// IF n.date = date from data replace count to value from data array
});
Thanks for any help
You can use _.find to match your items in the range to the ones in the data.
_.forEach(range, function(r) {
var d = _.find(data, {date: r.date}); //Find an item in "data" with date matching r.date
if(d) {
r.count = d.count;
}
});

Sorting a returned JSON array by date

Sorry if this is a really n00b question, but I am just getting into rails and can't figure this one out.
I am currently working with Analytics from a site. And want to put the data into a chart. However when I get the data back the stuff I want isn't in order. At the moment the data returned is:
{"legend_size":1,"data":{"series":["2013-06-20","2013-06-21","2013-06-22","2013-06-23","2013-06-24","2013-06-25","2013-06-26"],"values":{"undefined":{"2013-06-26":41,"2013-06-24":2,"2013-06-25":55,"2013-06-22":0,"2013-06-23":1,"2013-06-20":0,"2013-06-21":0}}}}
And what I need is the ["data"]["values"]["undefined"] bit to be ordered by date.
I know it's a simple question. But I have looked everywhere and can't figure it out.
Ok, as I said in the comment you can't really sort and object as it has an arbitrary order, so you must use an array.
My solution would be:
var objectFromAnalytics = {"2013-06-26":41,"2013-06-24":2,"2013-06-25":55,"2013-06-22":0,"2013-06-23":1,"2013-06-20":0,"2013-06-21":0},
toSort = [],
dateParts;
for (var date in objectFromAnalytics) {
dateParts = date.split("-");
toSort.push({ date: new Date(dateParts[0], (dateParts[1] - 1), dateParts[2]), value: objectFromAnalytics[date] });
}
var sorted = toSort.sort(function(a,b){
return a.date < b.date ? -1 : a.date > b.date ? 1 : 0; //you can change this to sort by value if you want
});
//sorted == [{ date: dateObject, value: value from analytics} , { ... }]
You can probably get away doing this in a few less lines, but I think is understandable this way :)

Categories

Resources