【发布时间】:2020-11-13 19:11:14
【问题描述】:
我正在创建一个购物清单应用程序,让用户能够创建购物清单。用户创建的每个购物清单都具有以下属性:
键 姓名 预算 列表项
listItem 属性中嵌套的是列表项对象的数组。
每个列表项都具有以下属性: ID: 姓名: 价格:
这是一个代码sn-p:
const [shoppingLists, setShoppingList] = useState([
{key: '1', name: `Khari's Shopping List`, budget: 200, listItems:[
{id: Math.random().toString(), name:"butter", price:2.00},
{id: Math.random().toString(), name:"milk", price:2.00},
{id: Math.random().toString(), name:"cereal", price: 3.00},
{id: Math.random().toString(), name:"chicken", price: 4.00},
{id: Math.random().toString(), name:"spaghetti", price: 5.00},
], },
{key: '2', name: `Janae's Shopping List`, budget: 500, listItems:[
{id: Math.random().toString(), name:"butter2", price:0.00},
{id: Math.random().toString(), name:"milk2", price:0.00},
{id: Math.random().toString(), name:"cereal3", price: 0.00},
{id: Math.random().toString(), name:"chicken2", price: 0.00},
{id: Math.random().toString(), name:"spaghetti2", price: 0.00},
], },
])
当用户在文本输入中输入数字时,我希望更新嵌套在列表项中的价格属性。关于如何实现这一目标的任何想法?
当用户在文本输入中输入数字时,我将根据 id 更新列表项数组中的价格属性。我不知道从哪里开始,我希望得到一些指导。感谢您提供的任何帮助。
【问题讨论】:
-
Math.random().toString()的东西是错误的,你应该修复它。 -
我知道这是错误的,此时@Adam 仅用于测试目的。
标签: javascript arrays reactjs react-native updating