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?