javascript - 对数组对象内的数组元素执行操作
问题描述
我试图找到两个元素的乘积之和,其中每个元素都在其自己的单独对象中。又名我试图在哪里找到 TotalValue:
TotalValue = Sum[Arrays.close * List.amtPurch]
Arrays 对象是从 API 中提取的,因此它采用这种时髦的格式,每个数组都嵌套了一层太深。这就是我无法访问每个元素以执行上述操作的地方
关于如何做到这一点的任何想法?
Arrays =
[
[
{
"date": "2021-04-01",
"minute": "12:34",
"label": "12:34 PM",
"high": 123.37,
"low": 123.3,
"open": 123.36,
"close": 123.31,
"average": 123.338,
"volume": 1519,
"notional": 187351.33,
"numberOfTrades": 16
}
],
[
{
"date": "2021-04-01",
"minute": "12:34",
"label": "12:34 PM",
"high": null,
"low": null,
"open": null,
"close": null,
"average": null,
"volume": 0,
"notional": 0,
"numberOfTrades": 0
}
],
[
{
"date": "2021-04-01",
"minute": "12:34",
"label": "12:34 PM",
"high": 676.5,
"low": 675.41,
"open": 676.21,
"close": 675.41,
"average": 675.977,
"volume": 1353,
"notional": 914597.545,
"numberOfTrades": 38
}
]
]
List: [
{ tick: "AAPL", amtPurch: 50, purchPrice: 10, datePurch: "01/01/2021"},
{ tick: "GOOG", amtPurch: "40", purchPrice: "10", datePurch: "01/01/2021"},
{ tick: "TSLA", amtPurch: "40", purchPrice: "10", datePurch: "01/01/2021"},
]
解决方案
const arrays =
[
[
{
"date": "2021-04-01",
"minute": "12:34",
"label": "12:34 PM",
"high": 123.37,
"low": 123.3,
"open": 123.36,
"close": 123.31,
"average": 123.338,
"volume": 1519,
"notional": 187351.33,
"numberOfTrades": 16
}
],
[
{
"date": "2021-04-01",
"minute": "12:34",
"label": "12:34 PM",
"high": null,
"low": null,
"open": null,
"close": null,
"average": null,
"volume": 0,
"notional": 0,
"numberOfTrades": 0
}
],
[
{
"date": "2021-04-01",
"minute": "12:34",
"label": "12:34 PM",
"high": 676.5,
"low": 675.41,
"open": 676.21,
"close": 675.41,
"average": 675.977,
"volume": 1353,
"notional": 914597.545,
"numberOfTrades": 38
}
]
];
const list = [
{ tick: "AAPL", amtPurch: 50, purchPrice: 10, datePurch: "01/01/2021"},
{ tick: "GOOG", amtPurch: "40", purchPrice: "10", datePurch: "01/01/2021"},
{ tick: "TSLA", amtPurch: "40", purchPrice: "10", datePurch: "01/01/2021"},
];
const result = arrays.reduce((acc, val, index) => acc + val[0].close * list[index].amtPurch, 0)
console.log(result);
推荐阅读
- mysql - 我想在我的 SQL 中添加一个触发器,以便它根据表本身的插入/删除操作来增加/减少计数器
- python - Python iCalendar 解析数据并写入另一个 ics 文件
- angular - 在 Jasmine 框架中使用 then 属性的角度测试路由器间谍
- java - 从改造响应中解析 JSON 对象
- python - pygame.error:WASAPI 无法初始化音频客户端:
- php - 当外键相同时如何让laravel知道2个(或多个)多对多关系不同
- git - Ubuntu git 备份文件
- passwords - 如何解决“sudo:无法更改过期密码:身份验证令牌操作错误”
- sql - SQL:使用 modify 函数迭代 xml 节点或如何递归地插入或修改节点
- ios - 使“可识别”协议向后兼容