Replace array entry with spread syntax in one line of code?

前端 未结 6 779
情深已故
情深已故 2021-02-04 00:02

I\'m replacing an item in a react state array by using the ... spread syntax. This works:

let newImages = [...this.state.images]
newImages[4] = updatedImage
this         


        
6条回答
  •  独厮守ぢ
    2021-02-04 00:47

    You can use map:

    const newImages = this.state.images
      .map((image, index) => index === 4 ? updatedImage : image)
    

提交回复
热议问题