1

I have an array formatted data input like so (mwe):

var trades = [{
  "trades": {
    "fields": {
      "orderNetPips": "33",
      "orderSymbol": "EURUSD",
    }
  }
}, {
  "trades": {
    "fields": {
      "orderNetPips": "33",
      "orderSymbol": "GBPUSD",
    }
  }
}, {
  "trades": {
    "fields": {
      "orderNetPips": "33",
      "orderSymbol": "AUDUSD",
    }
  }
}, {
  "trades": {
    "fields": {
      "orderNetPips": "33",
      "orderSymbol": "AUDUSD",
    }
  }
}, {
  "trades": {
    "fields": {
      "orderNetPips": "33",
      "orderSymbol": "EURUSD",
    }
  }
}, {
  "trades": {
    "fields": {
      "orderNetPips": "33",
      "orderSymbol": "EURUSD",
    }
  }
},

and would basically like to achieve the following:

  • grab the objects of the X (in this example two) most occuring orderSymbols (in this example EURUSD and AUDUSD)
  • make some calculations using their properties
  • push the returned sum from each of the calculations to new arrays in the following format

estimated outcome:

orderNetPips = [{x: 'EURUSD', y: sum}, {x: 'AUDUSD', y: sum}, ...,]
orderVolume  = [{x: 'EURUSD', y: sum}, {x: 'AUDUSD', y: sum}, ...,]
[...]
  • while the order of the orderSymbols within the new arrays must be equivalent
  • and should start from the most to the least occuring one from left to right

my very brief approach

// Create needed arrays

let orderNetPips = [];

// Loop all trades in trades-object, make calculations and push them to the new array
    trades.forEach(t => {
        orderCloseSymbol = t.fields.orderSymbol;
        orderNetPips = t.fields.orderVolume;

    // Grab the X (let's say 2) most occuring symbols and create an array with their order
    let mostOccuring = [];

    // ...loop and count
       // output: mostOccuring = [EURUSD, AUDUSD, ...]




    // Make calculations and push results to new arrays
       // ...loop and sum
       // output: orderNetPips = [{x: 'EURUSD', y: 99}, {x: 'AUDUSD', y: 66}, ...]

basic idea:

My idea would be to maybe first create a new array and define the most occuring orderSymbols to it in the required order. Then it might be possible to map the calculation results to the according orderSymbol within this array?

JSRB
  • 2,492
  • 1
  • 17
  • 48
  • Assuming you are able to do the calculations. You just have to order the way they are displayed (by orderSymbols). Can't you just count occurrences of each orderSymbols and assign it to your calculation in order to sort them? – grodzi Dec 23 '19 at 08:10
  • The calculations are well covered. So you mean to 1. search for the most occuring one, then use these objects to make the first calculations and map them to the output array, then take the second most occuring etc. ? – JSRB Dec 23 '19 at 08:15

1 Answers1

2

You can use reduce method to create desired objects and then sort an array by count property:

const obj = trades.reduce((a, c) => {
    let os = c.trades.fields.orderSymbol;
    a[os] = a[os] || {x: '', y: 0, count: 0};
    a[os].y += +c.trades.fields.orderNetPips;
    a[os].count += 1;
    a[os].x = os;
    return a;
}, {});

const result = Object.values(obj).sort((a, b) => b.count - a.count);

An example:

let trades = [{
    "trades": {
      "fields": {
        "orderNetPips": "33",
        "orderSymbol": "EURUSD",
      }
    }
  }, {
    "trades": {
      "fields": {
        "orderNetPips": "33",
        "orderSymbol": "GBPUSD",
      }
    }
  }, {
    "trades": {
      "fields": {
        "orderNetPips": "33",
        "orderSymbol": "AUDUSD",
      }
    }
  }, {
    "trades": {
      "fields": {
        "orderNetPips": "33",
        "orderSymbol": "AUDUSD",
      }
    }
  }, {
    "trades": {
      "fields": {
        "orderNetPips": "33",
        "orderSymbol": "EURUSD",
      }
    }
  }, {
    "trades": {
      "fields": {
        "orderNetPips": "33",
        "orderSymbol": "EURUSD",
      }
    }
  }];



const obj = trades.reduce((a, c) => {
    let os = c.trades.fields.orderSymbol;
    a[os] = a[os] || {x: '', y: 0, count: 0};
    a[os].y += +c.trades.fields.orderNetPips;
    a[os].count += 1;
    a[os].x = os;
    return a;
}, {});

const result = Object.values(obj).sort((a, b) => b.count - a.count);

console.log(result);
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • much appreciated for your support. I might run into trouble when trying to transfer this solution to my projects environment. Maybe I'll ping you once I stuck again :p – JSRB Dec 23 '19 at 08:52