react-redux - 当我钻入我的 redux 商店时变得不确定
问题描述
我正在使用 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}]
//I get undefined
console.log(items.totalAmount);
任何人都可以解释我做错了什么。
解决方案
items
当你这样做时看起来像这样console.log
:
[
{..., "totalAmount": 166.99},
{..., "totalAmount": 140},
...
]
因此,您可以访问totalAmount
每个项目,例如:items[0].totalAmount
要获取totalAmount
所有项目,您可以使用:
let sum = Array.from(items).reduce((acc, cur) => acc + cur.totalAmount, 0)
我也使用Array.from
了,因为没有保证项目是可序列化(正常)数组。(正如您确认它是不可序列化的。)
推荐阅读
- xslt - XSLT关于按多个元素分组并进行相应总结的问题
- .htaccess - 如果只有特定的 URL 匹配,则 URL 重定向 htaccess
- html - 当有人更新 html 表中的注释字段时保存到数据库(django)
- php - 无法通过 API 和 PHP Post 订阅 Mailchimp
- python - Pyspark SQL 在哪里?
- javascript - 当它在等待时返回null时如何处理mysql查询?
- javascript - 如何使用 JavaScript 和 Laravel 将我的小部件嵌入/渲染到外部网站
- java - 从二进制获得二进制补码十进制的方法?
- php - '$validate' 已声明但未使用。intelephense
- python-3.x - 如何在 Apple Silicon (ARM/M1) 上安装 TensorFlow 2 Object Detection API