“Destructuration des objets 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

comment cloner un objet en javascript

var sheep={"height":20,"name":"Melvin"};
var clonedSheep=JSON.parse(JSON.stringify(sheep));

//note: cloning like this will not work with some complex objects such as:  Date(), undefined, Infinity
// For complex objects try: lodash's cloneDeep() method or angularJS angular.copy() method
Grepper

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

objet javascript détruit

const employee = {name: ‘ANE01’, email: ‘[email protected]’, phone:’0112–345–6789'};
//with destucturing
const {name, email, phone} = employee;
//without destucturing
const name = employee.name;
const email = employee.email;
const phone = employee.phone;
Cute Civet

Destructuration des objets en javascript

const hero = {
  name: 'Batman'
};
// Object destructuring:
const { name: heroName } = hero;
heroName; // => 'Batman'
Dangerous Dunlin

Réponses similaires à “Destructuration des objets en javascript”

Questions similaires à “Destructuration des objets en javascript”

Plus de réponses similaires à “Destructuration des objets en javascript” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code