gpt4 book ai didi

javascript - 如何根据包含过去 12 个月的预定义数组将值映射到数组

转载 作者:行者123 更新时间:2023-12-01 03:03:45 25 4
gpt4 key购买 nike

我有一个包含几百个 trades 的数组像这样的对象:

输入:

[{
"trades": {
"model": "Trades_Information"
"fields": {
"orderCloseTime": "2019-03-19T16:20:56",
"orderOpenPrice": "1.40000",
"orderClosePrice": "1.44000",
"orderProfit": "75",
"orderLots": "1.4"
},
[...]
}]

现在我想循环所有对象并将结果(循环中包含的一些计算)映射到新数组,这将更新 Apexchart .

对我来说,现在最棘手的部分是动态地获得最近 12 个月的正确映射,因为每个新数组必须根据我创建的最近 12 个月数组以正确的顺序包含值,如下所示:
var date = new Date();
var lastMonths = [],
monthNames = ['Dec', 'Nov', 'Oct', 'Sep', 'Aug', 'Jul', 'Jun', 'May', 'Apr', 'Mar', 'Feb', 'Jan'];
for (var i = 0; i < 12; i++) {
lastMonths.push(monthNames[date.getMonth()]);
date.setMonth(date.getMonth() - 1);
}

console.log(lastMonths);

// ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

所以我以某种方式结束了这个简短的结构:
// Arrays needed for Apexchart
NetPipsMonth = [];
NetProfitMonth = [];
TotalVolumeMonth = [];
TotalTradesMonth = [];
lastMonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

// Loop all trades in trades-object
for (var i = 0; i < tradesTotal; i++) {

// Filter all trades by closed ones
closeTime = trades[i].fields.orderCloseTime;
if (closeTime != null) {

// Make some calculations that output one value per new array per trade object


// Push the results to each array in right order according to lastMonths array
// Mapping with `orderCloseTime`

}
}

不知道如何实现 orderCloseTime的映射为了将结果放在所需数组中的正确位置。

我在人类语言中的想法/想法:
  • 定义 orderCloseTime 的月份对于每笔交易,并使用 lastMonths 的顺序告诉 JS 新数组中的哪个位置是三月(上例)并通过此特定交易值的结果将其增加,以在循环所有交易对象后得到每月总和。
  •       lastMonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    NetProfitMonth = [ , , 75 , , ,];
    TotalVolumeMonth = [ , , 1.4 , , ... ,];
  • 也许创建一个嵌套对象并在循环时填充它以在一个主对象中包含所有值并为 apexchart 构建所需的数组循环结束后脱离这个主对象
  • 每个月构建包含每个计算的 12x4 循环,然后对结果求和,以获得包含在 4 个数组中的每月总和(最终会产生大量代码)

  • 期待您的想法,享受假期!

    最佳答案

    我会以不同的方式处理这个问题。我按正常顺序保持月份数组。 (1 月 1 日 12 月最后)。这将使其他数组的计算更容易。从日期获取月份,将其用作填充数组的索引。然后计算当月的轮换量,然后轮换你的数组。

    var tradesArr = [{
    "trades": {
    "model": "Trades_Information",
    "fields": {
    "orderCloseTime": "2019-03-19T16:20:56",
    "orderOpenPrice": "1.40000",
    "orderProfit": "75",
    "orderLots": "1.4"
    }
    }
    }, {
    "trades": {
    "model": "Trades_Information",
    "fields": {
    "orderCloseTime": "2019-04-19T16:20:56",
    "orderOpenPrice": "1.40000",
    "orderProfit": "75",
    "orderLots": "1.4"
    }
    }
    }, {
    "trades": {
    "model": "Trades_Information",
    "fields": {
    "orderCloseTime": "2019-03-19T16:20:56",
    "orderOpenPrice": "1.40000",
    "orderProfit": "75",
    "orderLots": "1.4"
    }
    }
    }, {
    "trades": {
    "model": "Trades_Information",
    "fields": {
    "orderCloseTime": "2019-05-19T16:20:56",
    "orderOpenPrice": "1.40000",
    "orderProfit": "75",
    "orderLots": "1.4"
    }
    }
    }, {
    "trades": {
    "model": "Trades_Information",
    "fields": {
    "orderCloseTime": "2019-11-19T16:20:56",
    "orderOpenPrice": "1.40000",
    "orderProfit": "75",
    "orderLots": "1.4"
    }
    }
    }, {
    "trades": {
    "model": "Trades_Information",
    "fields": {
    "orderCloseTime": "2019-12-19T16:20:56",
    "orderOpenPrice": "1.40000",
    "orderProfit": "75",
    "orderLots": "1.4"
    }
    }
    }];

    // taken from https://stackoverflow.com/a/1985471/12354911
    Array.prototype.rotate = (function() {
    var unshift = Array.prototype.unshift,
    splice = Array.prototype.splice;

    return function(count) {
    var len = this.length >>> 0,
    count = count >> 0;

    unshift.apply(this, splice.call(this, count % len, len));
    return this;
    };
    })();

    const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; // use this and keep ordering to later.
    // since the size is clear create array with size
    // fill them with 0 to claculate easier
    NetPipsMonth = Array(12).fill(0);
    NetProfitMonth = Array(12).fill(0);
    TotalVolumeMonth = Array(12).fill(0);
    TotalTradesMonth = Array(12).fill(0);




    tradesArr.forEach(t => {
    const fields = t.trades.fields;

    const ix = new Date(fields.orderCloseTime).getMonth(); // zero indexed month
    NetProfitMonth[ix] += +fields.orderProfit;
    TotalVolumeMonth[ix] += +fields.orderLots;
    // continute
    });

    // assume we are on may and we want may to be last item in the array
    // Date().getMonth() gave us 4 (zero indexed)
    const rotation = -((12 - 4)-1); // we need to rotate 7 to the right

    months.rotate(rotation);
    NetProfitMonth.rotate(rotation);
    TotalVolumeMonth.rotate(rotation);
    console.log(months, NetProfitMonth, TotalVolumeMonth);

    关于javascript - 如何根据包含过去 12 个月的预定义数组将值映射到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59442609/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com