Is it possible to get first 3 and last 2 items using array.slice in js
I want to render all data if user is AUTH, and if not I want first 3 and last 2 from array without recreating array.
{menu.slice(...(auth ? [0, 5] : //THERE SHOULD FIND FIRST 3 AND LAST 2 )).map((item) => (
<Link
onClick={() => dispatch(handleMenu())}
to={item.path}
className={`${auth === false && "font-md"} ${
item.path === window.location.pathname && "font-2xl"
}`}
>
{item.title}
</Link>
))}
Yes, it is possible to get first 3 items and last 2 items;
example:
let arr = ["a", "b", "d", "c", "e", "f", "g", "h", "i"];
To get first 3 items:
arr.slice(0, 3);
To get last 2 items:
arr.slice(-2);
As you wanted to add both of the slices and render them, this might help you:
arr.slice(0, 3).concat(arr.slice(-2));