React redux 当我钻入我的redux商店时变得不确定

React redux 当我钻入我的redux商店时变得不确定,react-redux,redux-toolkit,React Redux,Redux Toolkit,我正在使用useSelector检索我的存储。但是,当我console.log(items)有数据时,但是当我这样做console.log(items.totalAmount)时,我得到了未定义的数据 import {useSelector} from 'react-redux'; const items = useSelector(state => state.items); //I'm able to see the data console.log(items) 记录数据时会显

我正在使用useSelector检索我的存储。但是,当我
console.log(items)
有数据时,但是当我这样做
console.log(items.totalAmount)
时,我得到了未定义的数据

import {useSelector} from 'react-redux';

const items = useSelector(state => state.items);

//I'm able to see the data
console.log(items)
记录数据时会显示数据

[{"code": "SR71", "description": "Keyboard", "quantity": 1, "selling_price": 166.99, "totalAmount": 166.99}, {"code": "10", "description": "Cement", "quantity": 7, "selling_price": 20, "totalAmount": 140}]

请解释一下我做错了什么。

当你做
控制台时,项目看起来是这样的。log

[
  {..., "totalAmount": 166.99},
  {..., "totalAmount": 140},
  ...
]
因此,您可以访问每个项目的
totalAmount
,例如:
items[0]。totalAmount

要获取所有项目的
总金额
,您可以使用:

let sum = Array.from(items).reduce((acc, cur) => acc + cur.totalAmount, 0)

我还使用了
Array.from
,因为无法保证项是可序列化(正常)数组。(正如您确认它是不可序列化的。)

您可以添加
console.log(items)
打印的内容吗?当然,我会重新编辑问题undefined@TonyNgomana更新了答案。它现在包含完整的代码。确实在使用此代码吗?@TonyNgomana也尝试
console.log(items[0].totalAmount)
console.log(items[0].totalAmount+items[1].totalAmount)
。@TonyNgomana也尝试
console.log(Array.from(items)。reduce((acc,cur)=>acc+cur.totalAmount,0))
它是数组。from,请将它添加到答案中
let sum = Array.from(items).reduce((acc, cur) => acc + cur.totalAmount, 0)