refactor: do processing in one loop

This commit is contained in:
Lemuel 2022-03-11 19:41:21 +08:00
parent 7362e8d5e2
commit 910768f531

View File

@ -4,10 +4,10 @@ export default function heikinAshiDataset(columns: string[], data: Array<number[
const highIdx = columns.indexOf('high'); const highIdx = columns.indexOf('high');
const lowIdx = columns.indexOf('low'); const lowIdx = columns.indexOf('low');
// Prevent mutation of original data return data.map((original, idx, candles) => {
const dataCopy = data.map((original) => original.slice()); // Prevent mutation of original data
const candle = original.slice();
return dataCopy.map((candle, idx, candles) => {
if (idx === 0) { if (idx === 0) {
const close = (candle[openIdx] + candle[highIdx] + candle[lowIdx] + candle[closeIdx]) / 4; const close = (candle[openIdx] + candle[highIdx] + candle[lowIdx] + candle[closeIdx]) / 4;
const open = (candle[openIdx] + candle[closeIdx]) / 2; const open = (candle[openIdx] + candle[closeIdx]) / 2;