Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nested destructuring javascript

const a = {
  id: 1,
  name: 'Yogesh',
  age: 30,
  education: {
    degree: 'Bachelor'
  }
};
const {education: {degree}} = a;
console.log(degree); //prints: Bachelor
Comment

How to destructuring nested object value in javascript?

const person = {
    firstName: 'Adil',
    lastName: 'Arif',
    age: 25,
    job: 'web-developer',
    love: 'coding',
    friedsList: {
        friend1: 'Abir',
        friend2: 'Adnan'
    }
}
const { friend1, friend2 } = person.friedsList;
console.log(friend1, friend2);
//Expected output: Abir Adnan
Comment

es6 parameter destructuring nested object

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

myFunc({event: {target: {name: 'fred'}}})
Comment

Nested destructuring in 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
Comment

JavaScript nested destructuring

// 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]}
Comment

javascript Nested Destructuring Assignment

// 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
Comment

destructuring function nested parameters

let options = {
  title: 'My menu',
  items: ['item1', 'item2']
};

function showMenu({
  title = 'Untitled',
  width: w = 100,
  height: h = 200,
  items: [item1, item2] = ['something', 'esle'] // default whole array
  /*
  	items: [item1 = 'something', item2 = 'else']  // default items
  */
} = {} /* default empty obj if showMenu() doesn't get an argument */ ) {
  console.log(`${title} ${w} ${h}`);
  console.log(`${item1} and ${item2}`);
}

showMenu(options);
Comment

PREVIOUS NEXT
Code Example
Javascript :: Remove the minimum 
Javascript :: Return Instance Of Object 
Javascript :: pause media stream javascript 
Javascript :: JavaScript combining rows of multiple datasets 
Javascript :: wind in mongoose 
Javascript :: react with two components render empty 
Javascript :: multiple images gallery after clicking image javascript 
Javascript :: asynchronous file reading 
Javascript :: _.extend can be used to attach functions to a prototype like this 
Javascript :: React Native Component with Random Hexa 
Javascript :: _.extend Example 
Javascript :: json to css converter 
Javascript :: Add Methods to a Constructor Function Using Prototype 
Javascript :: unreachable code detected javascript 
Javascript :: onSeek video getting paused 
Javascript :: Listen to custom event in Vue.js 
Javascript :: Register Multiple Models In Admin 
Javascript :: Sorting Array of String, Numbers or Objects in javascript 
Javascript :: auto scrolling to end scrollview react native 
Javascript :: Is It Negative Zero (-0)? js 
Javascript :: select item from list javascript 
Javascript :: angularfire 
Javascript :: click page object 
Javascript :: javascript coding problems 
Javascript :: unminify javascript 
Javascript :: how to check my javascript code 
Javascript :: alternative of tofixed javascript 
Javascript :: nav hover add class and remove using javascript smooth 
Javascript :: check if content is overflowing react 
Javascript :: AngularJS Graphs & Charts - Mix of solid & dotted 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =