reactjs - 无法转换对象中值的数据类型
问题描述
我已经使用 mongoDB 作为数据库创建了 next.js 应用程序。
现在就像在我的代码中一样,当按钮被点击提交时,所有数据都将显示在表格中。我已经提取了我想要单独计算的所有数据,例如product_unit
和product_price
。但是,因为该值已push
作为productList
文本输入。我无法计算它。
我尝试使用.parseInt
转换,但它不起作用。
我应该怎么办?
我的代码:
const [jsxProductList, setJsxProductList] = useState(<tr></tr>);
const [productList, setProductList] = useState([]);
const onSubmitToDatabase = (data) => {
console.log({ productList })
fetch('/api/stock',
{
method: 'post',
body: productList
})
}
const onSubmit = (data) => {
let j = 1
for (let i = 1; i <= productList.length; i++) {
j++
}
var start_item_id = j
let p = { id: start_item_id, product_name: data.product_name, code: data.code, brand: 'Honda', model: 'CBR150', qty: data.qty, unitPrice: 100 }
productList.push(p)
console.log("productList", productList.length)
let newList = productList.map(p => {
console.log("Update JSX", p)
return (
<tr>
<td>{p.id}</td>
<td>{p.product_name}</td>
<td>{p.code}</td>
<td>{p.brand}</td>
<td>{p.model}</td>
<td>{p.qty}</td>
<td>{p.unitPrice}</td>
</tr>
)
})
setProductList(productList)
setJsxProductList(newList)
const product_code = []
const product_price = []
const product_unit = []
productList.map(p => {
product_code.push(p.code)
product_price.push(p.unitPrice)
product_unit.push(p.qty)
})
product_unit.map(h => (
h.parseInt
))
let total_unit, total_price = 0
for (let k = 0; k < product_unit.length; k++) {
total_unit = total_unit + product_unit[k]
}}
for (let k = 0; k < product_price.length; k++) {
total_unit = total_unit + product_unit[k]
}}
解决方案
尝试这个:
const new_product_unit = product_unit.map(h => parseInt(h));
// now use "new_product_unit" later in your code
推荐阅读
- c# - 在 DLL 'CIMWin32.dll' 中找不到名为“Reset”的入口点
- c++ - 更少的恒定时间迭代需要更多时间 - c++ 编译器依赖性?
- javascript - 每次我调用函数时防止声明空数组
- elasticsearch - Elasticsearch:仅搜索单词形式
- mysql - 如何将数字数组转换为 MySQL 中的时间戳?
- excel - 在新添加的行位置插入新行并从上一行复制公式
- html - 如何在下拉菜单中将宽度设置为 100%
- python - 如何在列表中打印特定范围的数字?Python
- java - 如何在java中对抽象类进行排序
- c++ - 在 C++ 中实例化类的最首选方法是什么?