Skip to content

Instantly share code, notes, and snippets.

@ApoloSiskos
ApoloSiskos / Group dictionary (array of objects) based on one object (key)
Created December 16, 2018 11:50
Group dictionary (array of objects) based on one object (key)
<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
@ApoloSiskos
ApoloSiskos / Datorama JSON - Group by number (variable) of dimensions (objects) and convert to a dictionary (array of objects) in Javascript
Last active December 16, 2018 11:41
Datorama JSON - Group by number (variable) of dimensions (objects) and convert to a dictionary (array of objects) in Javascript
//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) {
@ApoloSiskos
ApoloSiskos / Generate HTML Table from JSON (Javascript)
Last active October 15, 2018 11:56
Generate HTML Table from JSON (Javascript)
<!--<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>-->
@ApoloSiskos
ApoloSiskos / Group and convert JSON string to dictionary (array of objects) (Javascript)
Last active December 16, 2018 11:35
Datorama JSON - Group by First Dimension convert to dictionary (array of objects) with Javascript
//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)