“JavaScript Array Destructuration” 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

JS des tableaux et des objets destructeurs

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

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

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

JavaScript Array Destructuration

const arrValue = ['one', 'two', 'three'];

// destructuring assignment in arrays
const [x, y, z] = arrValue;

console.log(x); // one
console.log(y); // two
console.log(z); // three
SAMER SAEID

Destructeur JavaScript

// before you would do something like this
const person = {
    name: 'Sara',
    age: 25,
    gender: 'female'    
}

let name = person.name;
let age = person.age;
let gender = person.gender;

console.log(name); // Sara
console.log(age); // 25
console.log(gender); // female
SAMER SAEID

Array Destructurant JS

// In an array destructuring from an array of length N specified on the right-hand side of the assignment, if the number of variables specified on the left-hand side of the assignment is greater than N, only the first N variables are assigned values. The values of the remaining variables will be undefined.

const foo = ['one', 'two'];

const [red, yellow, green, blue] = foo;
console.log(red); // "one"
console.log(yellow); // "two"
console.log(green); // undefined
console.log(blue);  //undefined
Ill Iguana

Réponses similaires à “JavaScript Array Destructuration”

Questions similaires à “JavaScript Array Destructuration”

Plus de réponses similaires à “JavaScript Array Destructuration” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code