1

This is NOT a duplicate. I've already tried JSON.parse() and it doesn't work.
Here's my question: the code provided in this crucial & helpful answer I got yesterday produces a result which I cannot extract single values from, since it appears as a single continuum string of values:

this code

    var result = {};
  for ( var i = 0; i < arr.length; i++ ) {
  var key = "asset: " + arr[ i ].geo + ' ym: ' + arr[ i ].ym + ' venue: ' + arr[ i ].venue + ' value';
  result[ key ] = (result[ key ] || 0 ) + arr[ i ].value ;
    }
Logger.log(result);

transforms this array

    arr = [
{value:1.0, venue: "binance",  ym:20181.0, geo:"eur"}, 
{value:6.0, venue: "binance",  ym:20181.0, geo:"eur"},
{value:2.0, venue: "bitstamp", ym:20181.0, geo:"eur"}, 
{value:5.0, venue: "binance",  ym:20182.0, geo:"eur"}, 
{value:1.0, venue: "binance",  ym:20183.0, geo:"eur"}, 
{value:3.0, venue: "binance",  ym:20181.0, geo:"usd"}, 
{value:3.0, venue: "binance",  ym:20182.0, geo:"usd"}, 
{value:3.0, venue: "binance",  ym:20183.0, geo:"usd"}, 
{value:2.0, venue: "binance",  ym:20181.0, geo:"pop"}, 
{value:2.0, venue: "binance",  ym:20182.0, geo:"pop"}, 
{value:2.0, venue: "binance",  ym:20183.0, geo:"pop"}, 
{value:2.0, venue: "binance",  ym:20181.0, geo:"dot"}, 
{value:2.0, venue: "binance",  ym:20182.0, geo:"dot"}
];

into this continuous string of data

{asset: eur ym: 20181 venue: bitstamp value=2.0, asset: eur ym: 20182 venue: binance value=5.0, asset: dot ym: 20183 venue: binance value=2.0, asset: eur ym: 20183 venue: binance value=1.0, asset: usd ym: 20181 venue: binance value=3.0, asset: sol ym: 20183 venue: binance value=1.0, asset: pop ym: 20183 venue: binance value=2.0, asset: dot ym: 20182 venue: binance value=2.0, asset: dot ym: 20181 venue: binance value=2.0, asset: usd ym: 20183 venue: binance value=3.0, asset: sol ym: 20181 venue: binance value=1.0, asset: usd ym: 20182 venue: binance value=3.0, asset: cad ym: 20183 venue: binance value=3.0, asset: sol ym: 20182 venue: binance value=1.0, asset: pop ym: 20182 venue: binance value=2.0, asset: pop ym: 20181 venue: binance value=2.0, asset: cad ym: 20181 venue: binance value=3.0, asset: cad ym: 20182 venue: binance value=3.0, asset: eur ym: 20181 venue: binance value=7.0}

Which I don't know how to use since I can't extract specific values from it, like I would do in a standard array with, for instance, result[i].value or result[i].ym to return a specific key-value set.
Here it appears as a continuum where picking a specific part is not easy for me.
Even if Logger.log(typeof result); returns object I still can't extract any value from it. I can't thank enough the provider of this code and I know it's surely my fault and it's due probably to my lack of experience, but at the present time I don't know how to treat this result.

John Galassi
  • 309
  • 2
  • 16
  • 1
    What do you want to do? The keys are var key = "asset: " + arr[ i ].geo + ' ym: ' + arr[ i ].ym + ' venue: ' + arr[ i ].venue + ' value'.. So you could retrieve `Logger.log(result["asset: eur ym: 20181 venue: bitstamp value"])` for example. But not the individual values. – TheMaster Dec 29 '19 at 21:46
  • @TheMaster what I wanted to do is exactly this, may be you remember: https://stackoverflow.com/questions/59401936/sum-values-within-array-between-two-consecutive-limits-iteratively?noredirect=1#comment105046467_59401936. and until now this was the answer that most got near a valid solution to my question. I'll try your query nonetheless, thanks – John Galassi Dec 29 '19 at 21:53
  • thanks both **Cooper** and **Jon Trent** for your answers; let me try them and in case I hope i can ask for some clarifications. thanks again – John Galassi Dec 30 '19 at 12:27

2 Answers2

2

This is a Google Apps Script solution.

It reads in the array and calculates weekly totals and then outputs the input array and the weekly results on the active spreadsheet.

function sumArray() {
  var arr=[{value:1.0, venue: "binance",  ym:20181.0, geo:"eur"},{value:6.0, venue: "binance",  ym:20181.0, geo:"eur"},{value:2.0, venue: "bitstamp", ym:20181.0, geo:"eur"}, {value:5.0, venue: "binance",  ym:20182.0, geo:"eur"}, {value:1.0, venue: "binance",  ym:20183.0, geo:"eur"}, {value:3.0, venue: "binance",  ym:20181.0, geo:"usd"}, {value:3.0, venue: "binance",  ym:20182.0, geo:"usd"}, {value:3.0, venue: "binance",  ym:20183.0, geo:"usd"}, {value:2.0, venue: "binance",  ym:20181.0, geo:"pop"}, {value:2.0, venue: "binance",  ym:20182.0, geo:"pop"}, {value:2.0, venue: "binance",  ym:20183.0, geo:"pop"}, {value:2.0, venue: "binance",  ym:20181.0, geo:"dot"}, {value:2.0, venue: "binance",  ym:20182.0, geo:"dot"}];
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  sh.clearContents();//clear active sheet
  sh.appendRow(['value','venue','ym','geo']);//append headers for array
  arr.forEach(function(e,i){
    sh.appendRow([e.value,e.venue,e.ym,e.geo]);
  });//append array
  var vObj={};
  arr.forEach(function(e,i){
    if(!vObj.hasOwnProperty(e.venue)) {vObj[e.venue]={};}
    if(!vObj[e.venue].hasOwnProperty(e.ym)) {vObj[e.venue][e.ym]=e.value;}else{vObj[e.venue][e.ym]+=e.value;}  
    if(!vObj.hasOwnProperty(e.geo)) {vObj[e.geo]={};}
    if(!vObj[e.geo].hasOwnProperty(e.ym)) {vObj[e.geo][e.ym]=e.value;}else{vObj[e.geo][e.ym]+=e.value;}
    if(!vObj.hasOwnProperty('ym')) {vObj['ym']={};}
    if(!vObj['ym'].hasOwnProperty(e.ym)) {vObj['ym'][e.ym]=e.value;}else{vObj['ym'][e.ym]+=e.value;}
  });//calculate week sums
  //Logger.log(JSON.stringify(vObj));
  var keys=Object.keys(vObj.ym);
  keys.unshift('')
  sh.appendRow(keys);//headers for weekly sums
  var vkeys=Object.keys(vObj);
  //Logger.log(vkeys);
  for(var i=0;i<vkeys.length;i++) {
    var row=[];
    row.push(vkeys[i]);
    var rkeys=Object.keys(vObj[vkeys[i]]);
    for(var j=1;j<keys.length;j++) {
      if(vObj[vkeys[i]].hasOwnProperty(keys[j])) {
        row.push(vObj[vkeys[i]][keys[j]]);
      }else{
        row.push('');
      }
    }
    sh.appendRow(row);//appending weekly sum
  }
}

Here's an image of the output:

enter image description here

Cooper
  • 59,616
  • 6
  • 23
  • 54
1

Thanks for clarifying the problem. I think what you need to do is along the following lines.

 arr = [
  {value:1.0, venue: "binance",  ym:20181.0, geo:"eur"}, 
  {value:6.0, venue: "binance",  ym:20181.0, geo:"eur"},
  {value:2.0, venue: "bitstamp", ym:20181.0, geo:"eur"}, 
  {value:5.0, venue: "binance",  ym:20182.0, geo:"eur"}, 
  {value:1.0, venue: "binance",  ym:20183.0, geo:"eur"}, 
  {value:3.0, venue: "binance",  ym:20181.0, geo:"usd"}, 
  {value:3.0, venue: "binance",  ym:20182.0, geo:"usd"}, 
  {value:3.0, venue: "binance",  ym:20183.0, geo:"usd"}, 
  {value:2.0, venue: "binance",  ym:20181.0, geo:"pop"}, 
  {value:2.0, venue: "binance",  ym:20182.0, geo:"pop"}, 
  {value:2.0, venue: "binance",  ym:20183.0, geo:"pop"}, 
  {value:2.0, venue: "binance",  ym:20181.0, geo:"dot"}, 
  {value:2.0, venue: "binance",  ym:20182.0, geo:"dot"}
];

var result = {};
for ( var i = 0; i < arr.length; i++ ) {
  var key = '~' + arr[ i ].geo + '~' + arr[ i ].ym + '~' + arr[ i ].venue + '~';
  if ( result[ key ] === undefined ) {
    result[ key ] = {asset: arr[ i ].geo, ym: arr[ i ].ym, venue: arr[ i ].venue, subtotal: 0};
  }
  result[ key ].subtotal += arr[ i ].value;
}

console.log( result );

Basically, you still generate the key based on how you want to group the subtotals. But instead of the key/value pair being something like...

"~eur~20181~binance~": 7

...where the value is simply the subtotal, you make the value an object, and include the field values that are being grouped, in addition to the subtotal. So, the key/value pairs will now look like...

"~eur~20181~binance~": {asset: "eur", ym: 20181, venue: "binance", subtotal: 7}

...and now when you iterate through the results, you can reference the values that represent the group (ie, asset, ym, & venue in this case).

Hope this helps.

Trentium
  • 3,419
  • 2
  • 12
  • 19