简体   繁体   English

在使用扩展运算符修改 object 属性时,如何保留数组中 object 的索引?

[英]How can the index of an object within an array be preserved when modifying an object property while using the spread operator?

I am having a React useState-Variable that stores an Array of Objects which are of this Question type:我有一个 React useState-Variable,它存储一个属于这种问题类型的对象数组:

type Question = {
id: number,
text: string,
otherProps: string,
...... (and so on)
}

Example of my useState我的 useState 示例

const [questions, setQuestions] = React.useState<Question[]>([{id: 1, text: "hello?", otherProps: "Lorem Ipsum"}])

The order of these Question objects in the useState-Variable Array matters, so my question is: How should the following function be changed so that the text of the Question is changed but the array index of the modified object is maintained/kept?这些 Question 对象在 useState-Variable Array 中的顺序很重要,所以我的问题是:如何更改以下 function 以便更改 Question 的文本,但维护/保留修改后的 object 的数组索引? I am aware that currently I am first deleting the object and then placing a newly created on at the end, but I can't figure out another way right now.我知道目前我首先删除 object 然后在最后放置一个新创建的,但我现在想不出另一种方法。

function setQuestionTextById(id:number, text:string) {
    if (!questions) return;
    const question:Question|undefined = questions.find(x => x.id === id);
    if (!question) return;
    const newQuestion: Question = {
        ...question,
        text,
    };
    const filteredQuestions = questions.filter(item => item.id !== id);
    setQuestions([...filteredQuestions, newQuestion]);
}

You should use map on the array with a function that checks if the id is the one you want - if so it modifies it with the new text , otherwise leaves it as is.你应该在数组上使用map和 function 检查id是否是你想要的 - 如果是它用新text修改它,否则保持原样。

This way, your whole function becomes:这样,你的整个 function 就变成了:

function setQuestionTextById(id:number, text:string) {
    const updatedQuestions = questions.map(question => question.id === id ? { ...question, text } : question);
    setQuestions(updatedQuestions);
}

Which I find much more readable than the original, as well as preserving order as you say you need.我发现它比原来的更具可读性,并且可以根据您的需要保留顺序。

One further refinement would be to use the functional update form of setQuestions so it doesn't depend on the previous value of questions , but I'll leave that up to you - it may not matter, depending on how this is being used in your component.进一步的改进是使用setQuestions功能更新形式,因此它不依赖于questions的先前值,但我会把它留给你 - 这可能无关紧要,具体取决于它在你的成分。

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

相关问题 如何使用扩展运算符将属性添加到 Typescript 中的内部 Object ? - How can i add a property to an inner Object in Typescript with spread operator? 如何使用扩展运算符更改 object 的嵌套属性? - How to change nested property of an object using spread operator? 可以使用数组中的扩展运算符和 JavaScript 中的 object 找出差异 - Can figure out the difference using spread operator in array and object in JavaScript 如何使用扩展运算符使用扩展运算符使用反应从对象数组中删除 object? - How to use spread operator to delete an object from array of objects using spread operator using react? 使用扩展运算符的ES6对象克隆也在修改输入 - ES6 object cloning using spread operator is modifying input too 如何使用spread运算符从对象数组中删除重复项 - How to remove duplicates from an object array using spread operator 使用扩展运算符将对象添加到数组的开头 - Add object to the beginning of array using spread operator 使用传播运算符将对象推入数组吗? - Push an object into the array using spread operator? 如何使用扩展运算符将对象推入对象数组中 - How to push into an array of object using the spread operator to a specific element 使用传播算子向对象添加属性 - add property to object with spread operator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM