Skip to content

Instantly share code, notes, and snippets.

@codeBelt
Last active February 6, 2022 18:56
Show Gist options
  • Save codeBelt/7a2303c58de4252e2366138a89b68bb7 to your computer and use it in GitHub Desktop.
Save codeBelt/7a2303c58de4252e2366138a89b68bb7 to your computer and use it in GitHub Desktop.
const arrayData = ['a', 'b', 'c', 'd', 'e'];
/*
* destructuring array to object
*/
const { ...all } = arrayData;
all // {0: "a", 1: "b", 2: "c", 3: "d", 4: "e"}
/*
* destructuring array to object and renaming the index to a property name
*/
const { 0: first, 4: last, ...rest } = arrayData;
first // "a"
last // "e"
rest // {1: "b", 2: "c", 3: "d"}
/*
* destructuring array to object and renaming the index to a property name
* with last item being dynamic
*/
const { 0: first, [arrayData.length - 1]: last, ...rest } = arrayData;
first // "a"
last // "e"
rest // {1: "b", 2: "c", 3: "d"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment