Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

How to destructure arrays and objects in javascript?

// Array to destructure
const arr = ["Wissam", "Fawzi", "Darwich", "Fawaz"];
// First element, second element, and then remaining ones
const [firstName, middleName, ...restOfName] = arr;
console.log(firstName); // Wissam
console.log(middleName); // Fawzi
console.log(restOfName.join(" ")); // Darwich Fawaz

// Object to destructure
const socialMedia = {
  twitter: "@wissam_f",
  facebook: "https://www.facebook.com/wissam.fawaz",
  linkedin: "https://www.linkedin.com/in/wissam-fawaz-6b440839/",
};
// To extract values, property keys should be used
const {twitter, linkedin} = socialMedia;
// Twitter: @wissam_f
console.log("Twitter:", twitter);
// LinkedIn: https://www.linkedin.com/in/wissam-fawaz-6b440839/
console.log("LinkedIn:", linkedin);
Comment

js Destructuring arrays and objects

// 1) Destructure array items
const [first, second,, fourth] = [10, 20, 30, 40];

// 2) Destructure object properties
const { PI, E, SQRT2 } = Math;
Comment

js The equivalent of destructuring arrays and objects

// 1) assuming arr is [10, 20, 30, 40]
const first = arr[0];
const second = arr[1];
// third element skipped
const fourth = arr[3];

// 2)
const PI = Math.PI;
const E = Math.E;
const SQRT2 = Math.SQRT2;
Comment

PREVIOUS NEXT
Code Example
Javascript :: ngclass click change toggle 
Javascript :: time date utils 
Javascript :: javascript find in array cannot read property of undefined 
Javascript :: nodejs createwriteStream file image broken 
Javascript :: js download video element 
Javascript :: dict equivalent js 
Javascript :: react-native-modal big border 
Javascript :: check if the last character of word is "A" 
Javascript :: what is jsonpickle in python 
Javascript :: react-spring 
Javascript :: wind in mongoose 
Javascript :: javascript add content to array 
Javascript :: prisma single data fetch 
Javascript :: Constructor can also be written like this 
Javascript :: Example Of _.extend 
Javascript :: react axios request data objest from online json with table element 
Javascript :: Comparing mongoose _id and strings 
Javascript :: inject html string to div javascript 
Javascript :: telerik mvc grid add row 
Javascript :: save input local storage react 
Javascript :: calcular idade jquery 
Javascript :: js create an object from another object with some keys removed 
Javascript :: convert h2 to h1 jQuery 
Javascript :: inheritence in javascript 
Javascript :: Obtener url base 
Javascript :: how to get first and last 
Javascript :: unminify javascript 
Javascript :: react native class component short code 
Javascript :: Plumsail set form lookup field value on form load 
Javascript :: absolute item onPress ToucableOpacity problem 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =