Answers for "js destructuring"

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

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

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

destructuring objects

({ a, b } = { a: 10, b: 20 });
console.log(a); // 10
console.log(b); // 20


// Stage 4(finished) proposal
({a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40});
console.log(a); // 10
console.log(b); // 20
console.log(rest); // {c: 30, d: 40}
Posted by: Guest on May-08-2020
1

How to Destructuring Array in Javascript?

//destructuring array 
const alphabet = ['a', 'b', 'c', 'b', 'e'];
const [a, b] = alphabet;
console.log(a, b);
//Expected output: a b
Posted by: Guest on September-03-2021
1

Javascript array destructuring

let [a, b] = [9, 5]
[b, a] = [a, b]
console.log(a, b);
//Expected output: 5,9
Posted by: Guest on September-11-2021

Code answers related to "Objective-C"

Browse Popular Code Answers by Language