“Destrutura de objetos aninhados” Respostas de código

Destrutura de objetos aninhados

const obj = {a: {b: 'xyz'}};

const {a: {b}} = obj;

console.log(b); // xyz
Scriper

Array de destruição e objeto de um objeto aninhado

const person = {
    name: 'labib',
    age: 22,
    job: 'web-developer',
    frieds: ['ahsik', 'abir', 'alvi', 'hanafi'],
    childList: {
        firstChild: 'Salman',
        secondChild: 'Rafi',
        thirdChild: 'Anfi'
    }
}
const { frieds: [a, b, c] } = person; //array destructuring from a nested object 
console.log(a, b, c);
//expected output: ahsik abir alvi;
const { childList: { firstChild, secondChild } } = person; //object destructuring from a nested object
console.log(firstChild, secondChild)
//expected output:Salman Rafi
Ariful Islam Adil(Code Lover)

Destrutura aninhada em JavaScript

const person = {
    id: 0133,
    name: "Robert",
    positon: "web-developer",
    salary: 8000,
    pColor: "red",
    pSports: "football",
    pMovies: ["word war 1", "destroy the world", "long way", "Where is my home"],
    pChild: {
        firstChild: "Adiba",
        secondChild: "Alvi"
    }
}
const { secondChild } = person.pChild;
console.log(secondChild);
//Output: Alvi
Ariful Islam Adil(Code Lover)

ES6 Parâmetro Destructura objeto aninhado

const myFunc = ({event: {target: {name,secondName}}}) => {
  console.log(name);
  console.log(secondName);
}

myFunc({event: {target: {name: 'fred'}}})
Poised Petrel

Javascript aninhou -se a atribuição de destruição

// nested array elements
const arrValue = ['one', ['two', 'three']];

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

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

JavaScript aninhou -se a destruição

// Destructuring an object > array > object structure
const returnedObject = {
  docsArray: [
    {socketRoom: '', routes: []},
    {socketRoom: '', routes: []},
    {socketRoom: '', routes: []},
  ]
}

// this will destructure the first and second elements from docsArray, and spread the remainder
const {docsArray: [element0, element1, ...remainder]} = returnedObject

// taking this further with destructing properties from element0 
const {docsArray: [{socketRoom0, routes0} = element0, {socketRoom1, routes1} = element1]} = returnedObject

// note the syntax for destructing Objects and Arrays
const {propName} = Object
const [element0, element1] = Array

// then one layer deep where Object[propName] is an Array
const {propName: [element0]} = Object

// then two layers, where element0 is an Object
const {propName: [{propName} = element0]}

// three layers
const {propName: [{propName: [element00]} = element0]}
SubZ390

Respostas semelhantes a “Destrutura de objetos aninhados”

Perguntas semelhantes a “Destrutura de objetos aninhados”

Mais respostas relacionadas para “Destrutura de objetos aninhados” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código