I need better way to create arrays
Check my code:
const [first, second, third, four, ...others] = allItems;
return (
<div >
{[first, second, third, four].map().
</div>
)
I am using react but that is not important here.
I need to better a way to get first four item.
WHat is allItems?
This is array of object.
CodePudding user response:
You can use slice()
:
The
slice()
method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.
For example:
allItems.slice(0,4)
(And allItems.slice(4)
for the others)
CodePudding user response:
I think what you're looking for is allItems.slice(0,4)
.
return (
<div >
{allItems.slice(0,4).map()}.
</div>
)