score:1
Accepted answer
if you want to add the elements of the abilitiesdisplay
array to the client.abilities
array, you can create a new array that contains all the elements in both arrays by spreading them both.
example
class app extends react.component {
state = {
client: {
abilities: ["baz"]
},
abilitiesdisplay: ["foo", "bar"]
};
componentdidmount() {
settimeout(() => {
this.setstate(previousstate => ({
client: {
abilities: [
...previousstate.client.abilities,
...previousstate.abilitiesdisplay
]
}
}));
}, 2000);
}
render() {
return <div>{json.stringify(this.state)}</div>;
}
}
reactdom.render(<app />, document.getelementbyid("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>
score:0
you need this.state.abilitiesdisplay
.
score:1
try this =)
this.setstate((prevstate) => ({
client: {
abilities: [ ...this.prevstate.abilitiesdisplay ]
}
}));
i assume that you don't want to keep the old values in the abilities by your information above.
Source: stackoverflow.com
Related Query
- setState of an array to the values of another array
- How to check if property of an objects of array matches with one of the values in another array of object
- How to consecutively add values from one array to another and store the value in a new array in javascript?
- ReactJS - Swap out all the values in an array of objects in state with another array
- How to do a foreach for an array with values that have to match specific key and the same value from another array
- setState an array of Objects updating two values in the Object accordingly of the id
- Filtering two arrays based on another array values and also push the unavailable values into another array
- Append array of values to the current array in a state in React JS
- How do i iterate over an array of object in react.js and add the values of prices in the object as shown below
- Using redux-form, how can I reset the form values from another component?
- Create new array depending of the number of properties in another object in Javascript
- how to delete the formik values using field array
- React Hooks, updated value of setState shows initial values in another component
- Adding up the values of an array of objects javascript
- Updating the values in an array of objects in react js
- Fastest way to sort array of objects on the basis of nested key values
- How to use setState to splice into an array in the state?
- Using setState to change multiple values within an array of objects -- ReactJS
- React setState array is appending the same item twice on second call onward
- JavaScript array comparisions and display values from another
- Get object array from axios and using return value call another axios function and append the output to first result
- How to use setState to update the property of an object inside an array the right way in class component?
- How can I add checkbox values to setState array in reactJS
- Mirroring the position when moving objects from the array to the array to another object in React
- Why does the setState fn needs to be called through another function?
- Splitting string into array then using one of the values to be displayed in JavaScript
- DropDown menu is not showing values of the passed array
- Replace the array of elements using another array of elements if its have both common value
- How can I call another element from an array with the same name?
- How to create an array filter based on values of another array - React
More Query from same tag
- ReactJs: input is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML`
- Show more info in React
- How to make setState synchronous in React after axios response
- React render specific children
- ReactStrap NavLinks are broken when rendered via a component
- Map over API data in React
- How to persist only a subset of redux store into the local storage in redux-persist
- Can't show image in my ReactJs application
- Express res.download()
- Can you pass model attributes from an asp.net model into a reactjs.net component?
- deploying nextjs app to elastic beanstalk
- How to handle data fetch in react/redux component with propTypes?
- Reactjs, table cell with different background color
- How does one specify type (typeScript) on injected properties through withRouter?
- React App returns error after refresh
- calling method onChange in select attribute
- Why doesn't storybook use the same webpack config as in the project?
- How to resolve ' error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.'?
- findIndex problem with Immer and Redux Starter Kit
- How to add SPFX package larger than 100 MB?
- Why do I need curly braces for nested JSX, even if it is working without it
- React useEffect non-stop fetching
- NextJs Next.config.mjs import local constant file
- Style not updating in component - React
- lottie files reducing performance on NextJS app
- Why do I get a TypeError: this.getToken is not a function - React JS
- ReactJS, find elements by classname in a React Component
- Javascript difficulty consuming promise values even after using await
- React - dynamic input fields based on count
- How to resize width of Ant Design modal based on different screen sizes?