Let's assume that we have the following array that and we want to delete the fifth item:
javascriptlet numbers = [1, 2, 3, 4, 67, 5, 6, 7];
We can slice the array in two parts and then use the spread operator ...
to put everything together again:
javascript// We are slicing the array in two parts: 0 - 4 and 5 - End let first = numbers.slice(0, 4); let second = numbers.slice(5); // As we are not including the let completed = [...first, ...second]; // [1, 2, 3, 4, 5, 6, 7]
In one line of code:
javascriptlet completed = [...numbers.slice(0, 4), ...numbers.slice(5)]; // [1, 2, 3, 4, 5, 6, 7]
Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.