This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div style="background:green;" id="one"></div> | |
<script> | |
Array.prototype.groupBy = function(prop) { | |
return this.reduce(function(groups, item) { | |
const val = item[prop] | |
groups[val] = groups[val] || [] | |
groups[val].push(item) | |
return groups |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Group JSON string by a number of dimensions (number is a variable) and convert to a dictionary (array of objects) in Javascript | |
<div style="background:yellow; "id="original"></div> | |
<div style="background:red;" id="output"></div> | |
<script> | |
var json_data = {"headers":["Month","Value","Number"],"rows":[["2018-10-01 00:00:00.0","one",209],["2018-10-01 00:00:00.0","one",274],["2018-09-01 00:00:00.0","five",183],["2018-10-01 00:00:00.0","five",164],["2018-09-01 00:00:00.0","four",214],["2018-09-01 00:00:00.0","four",192]]}; | |
//const json_data = {Query.JSON}; | |
//Group by function | |
function groupBy(rows, numObjects) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--<script src="https://code.jquery.com/jquery-1.9.1.js"></script>--> | |
<table id="first_version"></table> | |
<table id="second_version"> | |
<tr><th>One</th><th>Two</th><th>Three</th></tr> | |
</table> | |
<!--<div id="third_version"></div>--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Convert a Datorama JSON string to a dictionary (array of objects) and group by the first dimension (first key). | |
//Ideal for D3 charts. The output can be used to present the SUM of all Values (Second Dimension in this case) per Date (First Dimension) | |
//////////////////// | |
//JSON query | |
var json_data = {"headers":["Month","Value","Number"],"rows":[["2018-10-01 00:00:00.0","one",209],["2018-09-01 00:00:00.0","one",274],["2018-09-01 00:00:00.0","five",183],["2018-10-01 00:00:00.0","five",164],["2018-09-01 00:00:00.0","four",214],["2018-10-01 00:00:00.0","four",192],["2018-09-01 00:00:00.0","three",128],["2018-10-01 00:00:00.0","three",125],["2018-09-01 00:00:00.0","two",199],["2018-10-01 00:00:00.0","two",169]]}; | |
function groupBy(accumulator, item) { | |
//Pick the key (values included in the key) |