首页 > 解决方案 > 对数组对象内的数组元素执行操作

问题描述

我试图找到两个元素的乘积之和,其中每个元素都在其自己的单独对象中。又名我试图在哪里找到 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"},
       ]

标签: javascriptreactjsobject

解决方案


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


推荐阅读