简体   繁体   English

更改对象中值的状态,在对象数组中的索引处,在对象中?

[英]Changing state of a value in an object, at an index within an array of objects, within a object?

I'm using React and the trying to use the setState hook to update a value in a quite nested object, as you might have understood from the title 😅我正在使用 React 并尝试使用 setState 钩子来更新嵌套对象中的值,正如您从标题中所理解的那样😅

I managed to get it to work somewhat, but instead of getting the original array of objects that I had before, I get an index object as my return value.我设法让它在某种程度上工作,但不是获得我以前拥有的原始对象数组,而是获得一个索引对象作为我的返回值。

So my object that i'm working with looks something like this:所以我正在使用的对象看起来像这样:

{
 category: ""
 level: ""
 name: ""
 rarity: ""
 requirements: [ {name: "", customValue: 0 <--- that I wish to update }, {name: "", customValue: 0 }]
}

When I tried to update the state of this value i managed to get it down to this.当我试图更新这个值的状态时,我设法把它归结为这个。 The index of the property is known.属性的索引是已知的。

setItemUpdated((prev) => ({
  ...prev,
  requirements: { ...prev.requirements, [idx]: { ...prev.requirements[idx], customValue: val } },
}));

This works, to some extent.这在某种程度上是有效的。 But instead of an [ {}, {} ] I'm getting [ 1:{} 2:{} ].但不是 [ {}, {} ] 我得到 [ 1:{} 2:{} ]。 I understand why I'm getting this, since I'm setting the first requirements as an object, however, when i'm trying to wrap the inside with an array, I'm never getting anything to compile.我明白为什么我会得到这个,因为我将第一个要求设置为一个对象,但是,当我尝试用数组包装内部时,我永远无法编译任何东西。 I really don't know how the syntax for that would be for that.我真的不知道它的语法是怎样的。

I tried to search for a similar thread here on SO which no doubt there is, but just couldn't find anything.我试图在 SO 上搜索类似的线程,这无疑是存在的,但找不到任何东西。 I'm hoping you can help me out here!我希望你能在这里帮助我!

Thank you in advance!先感谢您!

was actually working with something similar myself at the moment, the process i'm doing is copying out the state data, editing it based in the input (in your case called prev).目前我自己实际上正在使用类似的东西,我正在做的过程是复制状态数据,根据输入(在您的情况下称为 prev)进行编辑。 Tossing a quick potential answer out here since I just saw this question.因为我刚刚看到这个问题,所以在这里抛出一个快速的潜在答案。

const data = { ...useStateData.data };
data[input.name] = input.value;

setUseStateData({ data});

So you might need to do something like:因此,您可能需要执行以下操作:

data.requirements['name'] = updatedvalue;

Hope that helps -希望有帮助 -

As you already mentioned, you are trying to spread an array inside an object, which in result fill the object with the index/value pairs of the array.正如您已经提到的,您正在尝试在对象内传播一个数组,结果用数组的索引/值对填充对象。

I'd do instead something like following (in an immutable manner):我会做类似以下的事情(以不变的方式):

setItemUpdated(prev => ({
  ...prev,
  requirements: prev.requirements.map((prevRequirement, index) => index === idx
    ? {...prev.requirements[idx], customValue: val}
    : prevRequirement
  ),
}));

Which essentially maps over the prev.requirements and if it finds the known index, it replaces/modifies that object, otherwise it returns the prevRequirement from array它基本上映射了prev.requirements ,如果它找到已知索引,则替换/修改该对象,否则从数组返回prevRequirement

The long hand solution is to simply slice the array before and after the index and spread the object at the index.长手解决方案是简单地在索引前后对数组进行切片,并在索引处展开对象。

setItemUpdated((prev) => ({
  ...prev,
  requirements: [
    ...prev.requirements.slice(0, idx),
    { ...prev.requirements[idx], customValue: val },
    ...prev.requirements.slice(idx + 1),
  ],
}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM