【发布时间】:2020-09-22 05:34:57
【问题描述】:
我有一个数组。哪个名字是dynamicform,这个数组中的这个有对象
const dynamicform =[ {
id: Math.round(Math.random() * 36 ** 12).toString(36),
mainlabel: checklistlabel,
type: inputtype,
grid: inputgrid,
CheckLists: [
{
type: 'checkbox',
label: "Label one",
},
],
},
{
id:id: Math.round(Math.random() * 36 ** 12).toString(36),
name:"john",
type:"text"
}]
我只想在 CheckLists Array 中推送一个新对象,所以我写了这个
setDyanmicForm([
...dynamicform,
dynamicform.map((form, idx) =>
form.CheckLists.push({
type: 'checkbox',
label: defualtchecklabel,
})
)
]);
但显示错误
TypeError: 无法读取未定义的属性“push”
【问题讨论】:
-
您需要一些唯一标识符来跟踪您需要在哪个对象中更新值。你的 id 不是唯一的,这里面哪个字段是唯一的?
标签: javascript arrays reactjs