DEV Community

Discussion on: Destructuring Assignment in JavaScript

Collapse
 
avalander profile image
Avalander • Edited

You can also use the spread operator to partially destructure an array or object. For example, if you want to remove the first item of an array:

const [ head, ...rest ] = [ "pepper", "carrot", "onion" ]
rest // [ "carrot", "onion" ]

Same with objects:

const book = { title: "The Hobbit", author: "JRR Tolkien", year: 1937 }
const { title, ...bookWithoutTitle }
bookWithoutTitle // { author: "JRR Tolkien", year: 1937 }