score:1
Accepted answer
use the map()
method as shown below.
inside the callback function, check if the condition element.id === 3
is true; if it is, return a new object. otherwise, return the current element
as it is.
let newstate = arrayoftests.map((element) => {
if (element.id === 3) {
return { ...element, description: "new description" };
}
return element;
});
pass the array returned by the map()
method to the state setter function.
this.setstate({ state: newstate });
score:0
this.setstate({
arrayoftests: arrayoftests.map(item => {
if (item.id === 3) {
return {
...item,
description: 'new description'
}
}
return item
})
})
score:0
const [test, settest] = usestate([ { id: 1, name: "test1", description: "test description" }, { id: 2, name: "test2", description: "test description" }, { id: 3, name: "test3", description: "test description" } ]); const handleupdate = (index) => { const some_array = test; var newstate = some_array[index]; newstate.description = "new description"; some_array[index] = newstate; settest(some_array); console.log(test); } return( {test.length > 0 && test.map((data, index)=>{ return(handleupdate(index)}>{data.description}) }) } )
score:1
setstate((prevstate) => ({
...prevstate,
arrayoftests: prevstate.arrayoftests.map((item) =>
item.id === 3 ? { ...item, description: "new description" } : item
),
}));
Source: stackoverflow.com
Related Query
- How to use setState to update the property of an object inside an array the right way in class component?
- How do i use setState to update an object inside an array
- how do i update state in react for updating the value of an object inside an array using array.map function
- Use SetState to update one of the item in array object in react
- how to return element on the basis of an property of object which is inside the array
- How to update a single object inside the array with useReduce on state change
- How to change the property of an object inside an array propertly in react
- How to update an object in an array of Objects using setState
- How to sort array of object by properties from left to right having numbers first when property is string?
- I need to execute a function that will update the state right after using setState hook, but state inside function is empty?
- How to use setState to splice into an array in the state?
- How do i update value of a key in an object inside an Array in React?
- How to display the content of an array inside an object in Reactjs
- How can I render the contents of an array nested inside an object in React Admin's show/ArrayField component?
- How can I access JSON property of nested object inside object array
- Change the value of an object property name inside an array
- React Hooks, useState related question. How to update one property of object and keep the rest
- How to use setInterval inside useEffect to update the state?
- How to check if property of an objects of array matches with one of the values in another array of object
- How to iterate through the array inside json object
- How to update and target the right index in an array which is nested in an object?
- How to update the value present inside array of array in reactjs
- How can I use the varible inside array map to control style on Material UI?
- How to update an existing array of objects (add a new object to the array) with the useState hook?
- "Must use destructuring state assignment": How to destructure from object and place on property inside object literal
- Update property of certain object inside array in state
- How to use the property of an object as a source in the admin-on-rest?
- How do I update the state by updating an array within a particular object from an array of objects?
- How to update a useState which has an array, inside this array I have objects this objects will update when the input value will change in react js
- How to find the count of some object property in array of objects using react and javascript?
More Query from same tag
- How to properly do a dynamic route in reactJs when working with Add to Cart ecommerce functionality
- How to change the border color when error Helper text appears on TextField?
- How to add reactjs animation library and Animate .css to animate
- Why is VS code live server opening a directory instead of running the code in the browser?
- React Modal Component Using Functional Hooks
- Filtering an api response in reactjs
- Shallow render component and scryRenderedComponentsWithType
- How to build an npm package so it is able to be imported/required
- making array in map function
- Varying number of hooks for keyboard shortcuts
- For loop inside map React.js
- How would I move my button in an upward position?
- How to push the existing DOM element when a new element is created dynamically using javascript
- Reusability with Styled-Components in React
- Adding a svg line between 2 divs in flex box
- React-sortable-hoc with formData
- Typescript cryptic message when declaring function for one of my actions
- ReactJS how to display 5 page numbers at a time in pagination
- Using document.querySelector in React? Should I use refs instead? How?
- reactjs routing not working correctlly
- ReactJS Navbar not displaying correctly
- Rendering a redux component with and without children
- Typescript error connecting class component to the reducer
- How can I not pass children as props. Instead, nest children between the opening and closing tags on React?
- ReactJS 7 - How to conditionally change the background color of table cell only (not the row) based on its value?
- export object always {}
- Context compatible React hook for routing
- add link to json object in react js
- Persisting state across tables on event change
- Init UseState value from UseContext value