Search⌘ K
AI Features

Solution Review: Array Destructuring

Explore how to use array destructuring in JavaScript to extract and skip values efficiently. Understand the rest parameter for collecting remaining elements into an array. This lesson helps you apply these concepts by breaking down a solution that removes the first two items from an array.

We'll cover the following...

Solution

Javascript (babel-node)
function removeFirstTwo(list) {
const [, , ...arr] = list;
return arr;
}
var arrLiteral = [8,9,10,11,12]
console.log("arr contains: " + removeFirstTwo(arrLiteral))

Explanation

The solution to this is straightforward. Let’s go over it in detail and start by discussing the array destructuring syntax.

The following is an array literal:

var arrLiteral = [8,9,10,11,12]

Destructuring of an array uses similar syntax but on the left-hand side of the equation. This is to define which values we ...