Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Easiest, if you are using ES6.
ES6
initialArray = [1, 2, 3]; newArray = [ ...initialArray, 4 ];4]; // --> [1,2,3,4]
New array will be [1,2,3,4]
[1,2,3,4]
to update your state in React
this.setState({ arrayvar: [...this.state.arrayvar, newelement] });
Learn more about array destructuring
initialArray = [1, 2, 3]; newArray = [ ...initialArray, 4 ]; // --> [1,2,3,4]
this.setState({ arrayvar:[...this.state.arrayvar, newelement] });
initialArray = [1, 2, 3]; newArray = [...initialArray, 4]; // --> [1,2,3,4]
this.setState({arrayvar:[...this.state.arrayvar, newelement]});