“Destructeur en javascript” Réponses codées

Destructuration des objets

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;
Scary Snail

Destructuration des objets

Object Destructuring =>
//
   The destructuring assignment syntax is a JavaScript expression that makes it
possible to unpack values from arrays,
or properties from objects, into distinct variables.
//
example:
const user = {
    id: 42,
    is_verified: true
};

const {id, is_verified} = user;

console.log(id); // 42
console.log(is_verified); // true 
kepl3r

destructeur

var foo = ['one', 'two', 'three'];

// without destructuring
var one   = foo[0];
var two   = foo[1];
var three = foo[2];

// with destructuring
var [one, two, three] = foo;
Lovely Lion

Exemple de destructeur d'objets

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

// Object destructuring:
const { realName, address: { city } } = hero;
city; // => 'Gotham'
Nutty Narwhal

destructeur en javascript

/*
Array Destructuring: The following example shows us how to convert all 
the elements of an array to a variable.

Object Destructuring: The following example shows us how to convert all 
the properties of an object into a variable.
*/
//Array Destructuring
const friends = ['Bill', 'Gates'];
const [firstName, secondName] = friends;
console.log(secondName);  //Gates

//Object Destructuring
const person = {name: "Bill Gates", age: 17, phone: "123456789"};
const {name} = person;
console.log(name);  //Bill Gates
Tiny Coders

Destructeur en javascript

const { name, age, job } = { name: 'abrar', age: 24, job: 'web-developer' }
console.log(name, age, job)
Ariful Islam Adil(Code Lover)

Réponses similaires à “Destructeur en javascript”

Questions similaires à “Destructeur en javascript”

Plus de réponses similaires à “Destructeur en javascript” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code