data = [{timeInterval: {minute: '2022-03-17T15:00:00Z'}, volume: 0.05, high: 0.00001255389794727055, low: 0.00001255389794727055, open: '1.255389794727055'},{timeInterval: {minute: '2022-03-17T15:00:00Z'}, volume: 4, high: 0.000013573032220546603, low: 0.000013573032220546603, open: '1.35730322205466'}]
flattened= [['2022-03-17', '00:00:00.000Z', '386.6'], ['2022-03-18', '00:00:00.000Z', '393.5'],['2022-03-19', '00:00:00.000Z', '400']]
let bnb_token_ohlc = data.map(x=>
{return flattened.find(y=> (x.timeInterval.minute.substring(0, 10))==y[0])} )
console.log(bnb_token_ohlc)
(the response to the code above looks a bit weird on here, sorry I don't know how to make it look like it does on the browser console.)
The response I get now:
[[ "2022-03-17", "00:00:00.000Z", "386.6" ], [ "2022-03-17", "00:00:00.000Z", "386.6" ]]
Hello everyone. With this code I am able to match up the data, the .find works well. Now I want to put another .map within this code for y, I think that will help solve the issue below:
What I need to do, is to have javascript match up when data.timeInterval.minute=flattened[][0], and if these values are equal, take the flattened[3] value and multiply it by data.volume, data.high, data.low, and data.close and then return to me the array called data with these new values inside of it. Currently, I am able to just find when they are equal, but (I think) because the code has .map for data and not for flattened, I am not able to use y[3].
The expected output is something like this:
flattened [3] for the date 2022-03-17 is 386.6 so:
[{timeInterval: {minute: '2022-03-17T15:00:00Z'}, volume: 19.33, high: 0.00485333695, low: 0.00485333695, open: '484.580461},..]
I have been able to make javascript match up the code as you can see above, but now I need to get a value from a flattened array and multiply it by the values in the data array. I believe I need to do another .map inside of the one I have enough to get it for y, but I don't know how to do that.
Thanks for the help!
CodePudding user response:
You can try to use filter to get the multiplier, and use the spread syntax to copy the current element and overwrite the new value on the final return.
const data = [{
timeInterval: { minute: '2022-03-17T15:00:00Z' },
volume: 0.05,
high: 0.00001255389794727055,
low: 0.00001255389794727055,
open: '1.255389794727055'
}, {
timeInterval: { minute: '2022-03-17T15:00:00Z' },
volume: 4,
high: 0.000013573032220546603,
low: 0.000013573032220546603,
open: '1.35730322205466'
}];
const flattened = [
['2022-03-17', '00:00:00.000Z', '386.6'],
['2022-03-18', '00:00:00.000Z', '393.5'],
['2022-03-19', '00:00:00.000Z', '400']
];
const bnb_token_ohlc = data.map(datum => {
const minute = datum.timeInterval.minute.substring(0, 10);
const filtered = flattened.filter(a => a[0] === minute)[0];
const multiplier = filtered ? filtered[2] : 1;
return {
...datum,
volume: datum.volume * multiplier,
high: datum.high * multiplier,
low: datum.low * multiplier,
open: datum.open * multiplier,
}
});
console.log(bnb_token_ohlc);
If you still want to use find.
Replace
const filtered = flattened.filter(a => a[0] === minute)[0]; const multiplier = filtered ? filtered[2] : 1;
With
const found = flattened.find(a => a[0] === minute); const multiplier = found ? found[2] : 1;
const data = [{
timeInterval: { minute: '2022-03-17T15:00:00Z' },
volume: 0.05,
high: 0.00001255389794727055,
low: 0.00001255389794727055,
open: '1.255389794727055'
}, {
timeInterval: { minute: '2022-03-17T15:00:00Z' },
volume: 4,
high: 0.000013573032220546603,
low: 0.000013573032220546603,
open: '1.35730322205466'
}];
const flattened = [
['2022-03-17', '00:00:00.000Z', '386.6'],
['2022-03-18', '00:00:00.000Z', '393.5'],
['2022-03-19', '00:00:00.000Z', '400']
];
const bnb_token_ohlc = data.map(datum => {
const minute = datum.timeInterval.minute.substring(0, 10);
const found = flattened.find(a => a[0] === minute);
const multiplier = found ? found[2] : 1;
return {
...datum,
volume: datum.volume * multiplier,
high: datum.high * multiplier,
low: datum.low * multiplier,
open: datum.open * multiplier,
}
});
console.log(bnb_token_ohlc);
Or you can use reduce to sort the flattened array into the following format, so that you don't need to re-search the flattened array every time.
{
"2022-03-17": "386.6",
"2022-03-18": "393.5",
"2022-03-19": "400"
}
const data = [{
timeInterval: { minute: '2022-03-17T15:00:00Z' },
volume: 0.05,
high: 0.00001255389794727055,
low: 0.00001255389794727055,
open: '1.255389794727055'
}, {
timeInterval: { minute: '2022-03-17T15:00:00Z' },
volume: 4,
high: 0.000013573032220546603,
low: 0.000013573032220546603,
open: '1.35730322205466'
}];
const flattened = [
['2022-03-17', '00:00:00.000Z', '386.6'],
['2022-03-18', '00:00:00.000Z', '393.5'],
['2022-03-19', '00:00:00.000Z', '400']
];
const multiplierObj = flattened.reduce((accumu, current) => {
accumu[current[0]] = current[2];
return accumu;
}, {});
const bnb_token_ohlc = data.map(datum => {
const minute = datum.timeInterval.minute.substring(0, 10);
const found = multiplierObj[minute];
const multiplier = found ? found : 1;
return {
...datum,
volume: datum.volume * multiplier,
high: datum.high * multiplier,
low: datum.low * multiplier,
open: datum.open * multiplier,
}
});
console.log(bnb_token_ohlc);