繁体   English   中英

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

[英]How to map values to arrays according to a pre-defined array which contains the last 12 months

我有一个包含几百个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 个数组中的每月总和(最终会生成大量代码)

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

您确实可以按照您的建议创建数组。 的确,将三个值(月、利润、交易量)组合在一个对象中会更面向对象,但由于 apexChart 需要数组中的单个值,因此直接使用该结构是有意义的。

您可以使用一些splice魔法来创建月数组。

 let tradesTotal = [{ "trades": { "model": "Trades_Information", "fields": { "orderCloseTime": "2019-03-19T16:20:56", "orderProfit": "75", "orderLots": "1.4" }, } }]; let now = new Date(); now.setDate(32); // go to next month now.setDate(1); // set to first of next month now.setUTCHours(0, 0, 0, 0); // clear time let firstMonth = now.getMonth(); let lastMonths = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; lastMonths.splice(0, 0, ...lastMonths.splice(firstMonth)); // rotate let netProfitMonth = Array(12).fill(0); let totalVolumeMonth = Array(12).fill(0); let clone = new Date(now); clone.setFullYear(now.getFullYear()-1); let minDateString = clone.toJSON().slice(0, 10); for (let {trades: { fields: { orderCloseTime, orderProfit, orderLots }}} of tradesTotal) { if (orderCloseTime < minDateString) continue; // not in last 12 months: skip let month = (orderCloseTime.slice(5, 7) - firstMonth + 11) % 12; netProfitMonth[month] += +orderProfit; totalVolumeMonth[month] += +orderLots; } console.log(...lastMonths); console.log(...netProfitMonth); console.log(...totalVolumeMonth);

我会以不同的方式处理这个问题。 我按正常顺序保留我的月份数组。 (去年一月一日)。 这将使其他数组的计算更容易。 从日期获取月份,将其用作索引来填充数组。 然后计算当前月份的旋转量,然后旋转您的数组。

 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM