Answers for "destructuring javascript"

3

destructuring arrays with rest operator

// Rest operator on Arrays;
// It's usually shown as ...rest
// but you can name this what you like
// Think of it as "get the ...rest of the array"
const [q, r, ...callThisAnythingYouWant] = [1, 2, 3, 4, 5, 6, 7, 8];

console.log(q, r); // 1 2
console.log(callThisAnythingYouWant); // [ 3, 4, 5, 6, 7, 8 ]
Posted by: Guest on March-05-2020
39

javascript object destructuring

//simple example with object------------------------------
let obj = {name: 'Max', age: 22, address: 'Delhi'};
const {name, age} = obj;

console.log(name);
//expected output: "Max"

console.log(age);
//expected output: 22

console.log(address);
//expected output: Uncaught ReferenceError: address is not defined

// simple example with array-------------------------------
let a, b, rest;
[a, b] = [10, 20];

console.log(a);
// expected output: 10

console.log(b);
// expected output: 20

[a, b, ...rest] = [10, 20, 30, 40, 50];

console.log(rest);
// expected output: Array [30,40,50]
Posted by: Guest on May-16-2020
1

js object destructuring

const hero = {  
  name: 'Batman',  
  realName: 'Bruce Wayne'
};

const { name, realName } = hero;

name;     // => 'Batman',
realName; // => 'Bruce Wayne'
Posted by: Guest on October-09-2021
8

object destructuring javascript

// destructuring object & nested object & combine object into single object
let user = {
  name: 'Mike',
  friend: ["John", "Paul", "Jimmy"],
  location: {
    region:"England",
    country:"United Kingdom"
  },
  aboutMe: {
    status: "Single",
    pet: "Dog",
  }
}

const { name, friend, location, aboutMe: {status , pet} } = user;

console.log(name); // output: "Mike"
console.log(friend);  // output: [ 'John', 'Paul', 'Jimmy' ]
console.log(location); // output: { region: 'England', country: 'United Kingdom' }
console.log(status); // output: "Single"
console.log(pet); // output: "Dog"

//Combining Obj
const newUser = {
  ...user,
  car: {
    make: "Buick",
    year: 2012,
  }
}

console.log(newUser)
// output user obj + car object into one
// {
//   name: 'Mike',
//   friend: [ 'John', 'Paul', 'Jimmy' ],
//   location: { region: 'England', country: 'United Kingdom' },
//   aboutMe: { status: 'Single', pet: 'Dog' },
//   car: { make: 'Buick', year: 2012 }
// }

//Bonus destructuring from object of array
const {friend: [a, ...b]} = user
console.log(a) // output: "John"
console.log(b) // output: ["Paul", "Jimmy"]
Posted by: Guest on September-07-2021
8

object destructuring

const book = {
    title: 'Ego is the Enemy',
    author: 'Ryan Holiday',
    publisher: {
        name: 'Penguin',
        type: 'private'
    }
};

const {title: bookName =  'Ego', author, name: {publisher: { name }} = book, type: {publisher: { type }} = book } = book;
Posted by: Guest on March-18-2020
8

destructuring javascript

// destructuring array & nested array & combine array into single array
let person = ["John", "Sandy", "Sam", ["Mike", "Max"], "Diego", "Paul"];
// empty comma is like skipping array index. I skipped "Sam"
const [a, b, , c, ...d] = person;

let friend = [d, "Tom", "Jerry"] 
let newFriend = [...d, "Tom", "Jerry"]

console.log(a); // output: "John"
console.log(b); // output: "Sandy"
console.log(c); // output: [ "Mike", "Max" ]
console.log(d); // output: ["Diego", "Paul"]
console.log(friend); // output: [ [ 'Diego', 'Paul' ], 'Tom', 'Jerry' ]
console.log(newFriend); // output: [ 'Diego', 'Paul', 'Tom', 'Jerry' ]
Posted by: Guest on September-07-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language