Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在reactJs中使用useState更改嵌套对象中的值_Javascript_Arrays_Reactjs_Object - Fatal编程技术网

Javascript 在reactJs中使用useState更改嵌套对象中的值

Javascript 在reactJs中使用useState更改嵌套对象中的值,javascript,arrays,reactjs,object,Javascript,Arrays,Reactjs,Object,我正在尝试使用useState更改嵌套在对象中的数组中的对象属性的值 状态设置如下: const [groupState, setGroupState] = useState({ groupId: uuidv4(), content: [{ rowId: uuidv4(), field: '', from: '', to: '' }], }); 我正在尝试更改“field”属性的值。如果我在required属性中添加一个具有所需值的完整“dummy”对象,那么它将起作用

我正在尝试使用useState更改嵌套在对象中的数组中的对象属性的值

状态设置如下:

  const [groupState, setGroupState] = useState({
    groupId: uuidv4(),
    content: [{ rowId: uuidv4(), field: '', from: '', to: '' }],
  });
我正在尝试更改“field”属性的值。如果我在required属性中添加一个具有所需值的完整“dummy”对象,那么它将起作用:

setGroupState({
  ...groupState,
  content: groupState.content.map((item) => {
    if (item.rowId === rowId) {
      return (item = { rowId: uuidv4(), field: value, from: '', to: '' });
    }
  }),
});
但是,我只需要更新特定的属性值,而不是整个对象。我最近的努力如下:

setGroupState({
  ...groupState,
  content: groupState.content.map((item) => {
    if (item.rowId === rowId) {
      return (item.field = value);
    }
  }),
});

返回错误:“无法在字符串“user”上创建属性“field”-我做错了什么,但我不知道是什么原因。

尝试销毁项目对象:

setGroupState({
  ...groupState,
  content: groupState.content.map((item) => ({
    ...item,
    field: item.rowId === rowId ? value : item.field,
  })),
});

// Without explicit return
setGroupState({
  ...groupState,
  content: groupState.content.map((item) => {
    return { ...item, field: item.rowId === rowId ? value : item.field };
  }),
});

好的,明白了,谢谢丹尼斯·瓦什-只是需要一点改变:

setGroupState({
  ...groupState,
  content: groupState.content.map((item) => ({
    ...item,
    field: item.rowId === rowId ? value : item.field,
  })),

关闭但没有雪茄,它可以工作,但当您添加另一行时,前几行中的字段值消失,需要找到一种方法删除三元中的else值,但再次出现一系列错误。%&&%$£! :-)没有人能猜到你的应用程序是如何工作的,我们会用我们掌握的信息回答你的具体问题。