codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Follow publication

Destructuring JavaScript Arrays Object(ively)

Glad Chinda
codeburst
Published in
7 min readAug 3, 2020

--

TL;DR

// Array to be destructured
const RGB = [255, 180, 151];
// Array Destructuring
const [red, green, blue] = RGB;
// Object Destructuring (equivalent)
const {0: red, 1: green, 2: blue} = RGB;

Background

// Array that will be destructured
const RGB = [255, 180];
// Array Destructuring
// Skip the first item
// Set a default value (51) for the third item (`blue`)

const [, green, blue = 51] = RGB;
console.log(green); // 180
console.log(blue);
// 51
// Object that will be destructured
const AUTHOR = {

name: "Glad Chinda",
lang: "JavaScript"

};
// Object Destructuring
// Declare a local variable (`language`) for `lang`
// Set a default value (16) for `age`

const {name, lang: language, age = 16} = AUTHOR;
console.log(name); // "Glad Chinda"
console.log(lang);
// undefined
console.log(language);
// "JavaScript"
console.log(age);
// 16

Array Destructuring

// Variables to be swapped
let width = 1280;
let height = 720;
// Swapping Variables (without array destructuring)
// A temporary variable is required

let temp = width;
width = height;
height = temp;
// Swapping Variables (with array destructuring)
[width, height] = [height, width];
// Array to be cloned
const scores = [23, 27, 22, 41, 35];
// Clone the `scores` array
const [...scoresClone] = scores;
console.log(scoresClone); // [23, 27, 22, 41, 35]
console.log(scores === scoresClone); // false
// Destructuring a String
const [firstChar, ...otherChars] = "Hello";
// Destructuring a Set
const [,, thirdItem] = new Set([1, 2, 3, 4, 5]);
console.log(firstChar); // "H"
console.log(otherChars); // ["e", "l", "l", "o"]
console.log(thirdItem); // 3

Limitations of Array Destructuring

// METHOD 1 — Using Item Index
// The 3rd item will be at index 2

const thirdItem = ITEMS[2];
// METHOD 2 — Using Array Destructuring
// Simply skip the first two items

const [,, thirdItem] = ITEMS;
// Accessing the last item in the ITEMS array
const lastItem = ITEMS[ITEMS.length — 1];
// Throws a SyntaxError
// Rest parameter element must always be the last element

const [...skippedItems, lastItem] = ITEMS;

Object Destructuring to the Rescue

// Array to be destructured
const RGB = [255, 180, 151];
// Array Destructuring
const [red, green, blue] = RGB;
// Object Destructuring (equivalent)
const {0: red, 1: green, 2: blue} = RGB;
// Using object destructuring
// The last(100th) item will be at index 99

const { 99: lastItem } = ITEMS;
// Using object destructuring
// ITEMS array contains 100 items

const {
0: firstItem,
2: thirdItem,
49: middleItem,
99: lastItem
} = ITEMS;
// Using object destructuring
// Order of index properties does not matter

const {
2: thirdItem,
99: lastItem,
49: middleItem,
0: firstItem
} = ITEMS;

More with Object Destructuring

// Using object destructuring
// Get the first item and the length

const { 0: firstItem, length } = ITEMS;
// Using object destructuring
// Get the first, middle and last items

const {
0: firstItem,
[ITEMS.length — 1]: lastItem
,
[Math.floor(ITEMS.length / 2)]: middleItem
} = ITEMS;
// Using object destructuring
// Get the length and first, middle and last items

const {
length,
0: firstItem,
[length — 1]: lastItem
,
[Math.floor(length / 2)]: middleItem
} = ITEMS;
// Using object destructuring
// Get the length and first, middle and last items
// The length is assigned to the `size` variable

const {
length: size,
0: firstItem,
[size — 1]: lastItem
,
[Math.floor(size / 2)]: middleItem
} = ITEMS;

Going Beyond Arrays

// String to be destructured
const STRING = "Hello World!";
// Object destructuring for string
const {length, [length — 1]: lastChar} = STRING;
console.log(length); // 12
console.log(lastChar); // "!"

📖 Further Reading

Conclusion

Your Feedback

--

--

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Written by Glad Chinda

Software engineer and web technology enthusiast — striving towards building better web applications one day at a time.

No responses yet

Write a response