0

I have an array of object like

const test = [{id: 1, text: "Test"}, { id: 2 , text: "Test 2" } ]
const index =  test.findIndex((listItem) => listItem === test[0])

Here I am trying to update the value of text key using the index.

I have a solution like this:

 const editItemText = (value) => {
    const newList = replaceItemAtIndex(todoList, index, {
      ...item,
      text: value,
    });

    setTodoList(newList);
  };
  
const replaceItemAtIndex =  [...arr.slice(0, index), newValue, ...arr.slice(index + 1)];

is there any other option to do so ? Also How does the given solution works ?

Thanks.

ganesh kaspate
  • 1
  • 9
  • 41
  • 88
  • Can you update the code and add [minimal reproducible code](https://stackoverflow.com/help/minimal-reproducible-example), so that people can understand your problem clearly. – DecPK Dec 08 '21 at 03:56
  • First you have to find the index and then use ```let index = 1; // This is the index value. test[index]['text']='New text value';``` – Andrew Rayan Dec 08 '21 at 03:59

0 Answers0