Home > Back-end >  What is the best way to replace an item from an array with some custom jsx in React
What is the best way to replace an item from an array with some custom jsx in React

Time:01-20

In my React component I receive an array of objects (6 items). I have to replace the 6th item with some custom jsx.

What's the best way to achieve this?

CodePudding user response:

array[array.length-1] = <div>Hello</div>

if you want something better you have to give me more to work with.

CodePudding user response:

I would recommend you learn methods for working with arrays. There a lot of approaches. It's one from them

const myArray = [{item1}, {item2}, {item3}, {item4}, {item5}, {item6}];
<div>
    {myArray.slice(1,5).map((item, index) =>  item)}
</div>
  • Related