遍历数组和映射

时间:2019-08-07 18:39:36

标签: javascript

“我正在尝试获取每一天的totalAmount工具。我有一个对象数组,其中包含InstrumentId和dailyPrices作为键。InstrumentId只是一个键值对。每日价格键是一个具有键为日期和值作为价格。我想通过计算时间量向对象添加一个键作为totalAmount(数量*该日期的价格)。“

“我已经尝试通过仪器ID和日期获取价格对数组进行分组,但是它变得越来越复杂。我还尝试了underscore.js中的多种方法。”

let priceData = [{
    instrumentId: 7138,
    dailyPrices: {
      2019 - 02 - 01: 15.89,
      2019 - 02 - 02: 93.990,
      2019 - 02 - 03: 80.90
    }
  },
  {
    instrumentId: 7132,
    dailyPrices: {
      2019 - 02 - 01: 10.89,
      2019 - 02 - 02: 23.990,
      2019 - 02 - 03: 87.90
    }
  }
]

let responseObject = {
  2019 - 02 - 01: {
    7132 - 65: {
      instrumentId: 7132,
      quantity: 10
    },
    7138 - 69: {
      instrumentId: 7138,
      quantity: 18
    }
  },
  2019 - 02 - 03: {
    7132 - 65: {
      instrumentId: 7132,
      quantity: 13
    },
    7138 - 69: {
      instrumentId: 7138,
      quantity: 15
    }
  }

}

我想要这样的输出

let responseObject = {
  2019 - 02 - 01: {
    7132 - 65: {
      instrumentId: 7132,
      quantity: 10,
      totalAmount: 108.9
    },
    7138 - 69: {
      instrumentId: 7138,
      quantity: 18,
      totalAmount: 286.02
    }
  },
  2019 - 02 - 03: {
    7132 - 65: {
      instrumentId: 7132,
      quantity: 13,
      totalAmount: 1142.7
    },
    7138 - 69: {
      instrumentId: 7138,
      quantity: 15,
      totalAmount: 1213.5
    }
  }

}

3 个答案:

答案 0 :(得分:0)

您可以输入responseObject的条目并通过寻找价格来生成想要的财产。

最后重建新对象。

var priceData = [{ instrumentId: 7138, dailyPrices: { '2019-02-01': 15.89, '2019-02-02': 93.990, '2019-02-03': 80.90 } }, { instrumentId: 7132, dailyPrices: { '2019-02-01': 10.89, '2019-02-02': 23.990, '2019-02-03': 87.90 } }],
    responseObject = { '2019-02-01': { '7132-65': { instrumentId: 7132, quantity: 10 }, '7138-69': { instrumentId: 7138, quantity: 18 } }, '2019-02-03': { '7132-65': { instrumentId: 7132, quantity: 13 }, '7138-69': { instrumentId: 7138, quantity: 15 } } },
    result = Object.fromEntries(Object
        .entries(responseObject)
        .map(([k, o]) => [
            k,
            Object.fromEntries(Object
                .entries(o)
                .map(([l, q]) => [
                    l,
                    { ...q, totalAmount: q.quantity * priceData.find(p => q.instrumentId === p.instrumentId).dailyPrices[k] }
                ]))
        ]));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

我不太确定您如何计算totalPrice。看起来totalPricequantitydailyPrice相乘而得出的。{p1

顺便说一下迭代技术:

  • 可以使用普通的for loop[...array[.forEach(element =>{})来迭代数组。
  • 可以使用Object.keys(map).forEach(key=>{})来迭代Map

这是可运行的代码:

let priceData = [{ instrumentId: 7138, dailyPrices: { "2019 - 02 - 01": 15.89, "2019 - 02 - 02": 93.990, "2019 - 02 - 03": 80.90 } }, { instrumentId: 7132, dailyPrices: { "2019 - 02 - 01": 10.89, "2019 - 02 - 02": 23.990, "2019 - 02 - 03": 87.90 } }];
let responseObject = { "2019 - 02 - 01": { "7132 - 65": { instrumentId: 7132, quantity: 10 }, "7138 - 69": { instrumentId: 7138, quantity: 18 } }, "2019 - 02 - 03": { "7132 - 65": { instrumentId: 7132, quantity: 13 }, "7138 - 69": { instrumentId: 7138, quantity: 15 } } };
let priceIndex = {};
// Index prices by day and instrument id for quick access
[...priceData].forEach(price => {
    Object.keys(price.dailyPrices).forEach((key) => priceIndex[key + "-" + price.instrumentId] = price.dailyPrices[key])
});
Object.keys(responseObject).forEach(day => {
    Object.keys(responseObject[day]).forEach(token => {
        responseObject[day][token]['totalAmount'] = responseObject[day][token]['quantity'] * priceIndex[day + "-" + responseObject[day][token]['instrumentId']];
    });
});
console.log(responseObject);

答案 2 :(得分:0)

我首先将数组更改为哈希表-因为您拥有键及其浪费的计算来搜索键O(ln)而不是O(1)

然后遍历响应并将其添加到列表或新列表中(例如):

提醒一下:Public Property TrackLength As TimeSpan遍历所有可迭代对象-也是继承的

for ... in
let iterationCounter = 0;

let priceData = [{
    instrumentId: 7138,
    dailyPrices: {
      "2019 - 02 - 01": 15.89,
      "2019 - 02 - 02": 93.990,
      "2019 - 02 - 03": 80.90
    }
  },
  {
    instrumentId: 7132,
    dailyPrices: {
      "2019 - 02 - 01": 10.89,
      "2019 - 02 - 02": 23.990,
      "2019 - 02 - 03": 87.90
    }
  }
]
const flattedPriceData = {}
for(let {instrumentId, dailyPrices} of priceData){

  flattedPriceData[+instrumentId] = dailyPrices;
  iterationCounter++;
}


let responseObject = {
  "2019 - 02 - 01": {
    "7132 - 65": {
      instrumentId: 7132,
      quantity: 10
    },
    "7138 - 69": {
      instrumentId: 7138,
      quantity: 18
    }
  },
  "2019 - 02 - 03": {
    "7132 - 65": {
      instrumentId: 7132,
      quantity: 13
    },
    "7138 - 69": {
      instrumentId: 7138,
      quantity: 15
    }
  }

}

const resultResponse = {}
for(let currentDate in responseObject){
    const subObject = responseObject[currentDate]
    resultResponse[currentDate] = {}
    for(let weirdInstrumentId in subObject){
      iterationCounter++; 
      const subsubObject = subObject[weirdInstrumentId]
      resultResponse[currentDate][weirdInstrumentId] = {...subsubObject, cost: subsubObject.quantity * flattedPriceData[subsubObject.instrumentId][currentDate]}
    }
}

console.log(resultResponse)
console.log(iterationCounter, "iterations for 2 instruments and 2 days (2 + 2 * 2 - O(n)")