Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

object destructuring javascript

// destructuring object & nested object & combine object into single object
let user = {
  name: 'Mike',
  friend: ["John", "Paul", "Jimmy"],
  location: {
    region:"England",
    country:"United Kingdom"
  },
  aboutMe: {
    status: "Single",
    pet: "Dog",
  }
}

const { name, friend, location, aboutMe: {status , pet} } = user;

console.log(name); // output: "Mike"
console.log(friend);  // output: [ 'John', 'Paul', 'Jimmy' ]
console.log(location); // output: { region: 'England', country: 'United Kingdom' }
console.log(status); // output: "Single"
console.log(pet); // output: "Dog"

//Combining Obj
const newUser = {
  ...user,
  car: {
    make: "Buick",
    year: 2012,
  }
}

console.log(newUser)
// output user obj + car object into one
// {
//   name: 'Mike',
//   friend: [ 'John', 'Paul', 'Jimmy' ],
//   location: { region: 'England', country: 'United Kingdom' },
//   aboutMe: { status: 'Single', pet: 'Dog' },
//   car: { make: 'Buick', year: 2012 }
// }

//Bonus destructuring from object of array
const {friend: [a, ...b]} = user
console.log(a) // output: "John"
console.log(b) // output: ["Paul", "Jimmy"]
Comment

javascript object destructuring

//simple example with object------------------------------
let obj = {name: 'Max', age: 22, address: 'Delhi'};
const {name, age} = obj;

console.log(name);
//expected output: "Max"

console.log(age);
//expected output: 22

console.log(address);
//expected output: Uncaught ReferenceError: address is not defined

// simple example with array-------------------------------
let a, b, rest;
[a, b] = [10, 20];

console.log(a);
// expected output: 10

console.log(b);
// expected output: 20

[a, b, ...rest] = [10, 20, 30, 40, 50];

console.log(rest);
// expected output: Array [30,40,50]
Comment

js object destructuring

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

const { name, realName } = hero;

name;     // => 'Batman',
realName; // => 'Bruce Wayne'
Comment

object destructuring

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

javascript deconstruct object

const objA = {
 prop1: 'foo',
 prop2: {
   prop2a: 'bar',
   prop2b: 'baz',
 },
};

// Deconstruct nested props
const { prop1, prop2: { prop2a, prop2b } } = objA;

console.log(prop1);  // 'foo'
console.log(prop2a); // 'bar'
console.log(prop2b); // 'baz'
Comment

destructuring an object js

const user = { id: 42, isVerified: true }

// grabs the property by name in the object, ignores the order
const { isVerified, id } = user;

console.log(isVerified);
// > true
Comment

Object destructuring

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 
Comment

object destructuring

let a, b, rest;
[a, b] = [10, 20];

console.log(a);
// expected output: 10

console.log(b);
// expected output: 20

[a, b, ...rest] = [10, 20, 30, 40, 50];

console.log(rest);
// expected output: Array [30,40,50]
Comment

js object destructuring

const { [propName]: identifier } = expression;
Comment

how to use object destructuring

// Noob [ not good ]
function getFullName(userObj) {
  const firstName = userObj.firstName;
  const lastName = userObj.lastName;

  return `${firstName} ${lastName}`;
}

// master [ yap little bit ]
function getFullName(userObj) {
  const { firstName, lastName } = userObj;
  return `${firstName} ${lastName}`;
}

// hacker [ i prefer this way ]
function getFullName({ firstName, lastName }) {
  return `${firstName} ${lastName}`;
}
// example func call
getFullName({
	firstName: 'John',
    lastName: 'Duo'
});
Comment

object destructuring in javascript

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

javascript object destructuring

// In this syntax:

let { property1: variable1, property2: variable2 } = object;

// The identifier before the colon (:) is the property of the object and the identifier after the colon is the variable.
Comment

destructuring an object in JS

let person = {
    firstName: 'John',
    lastName: 'Doe'
};
Code language: JavaScript (javascript)
Comment

object destructuring

const user = {
  name: "Jenny",
  age: "12",
  hobbies: ["Sport", "Programmieren", "essen"],
};

const { age } = user;
console.log(age);
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript change right click menu 
Javascript :: How to convert a canvas to an image javascript 
Javascript :: vim go back word 
Javascript :: add and get tokens to securestore expo 
Javascript :: javascript optional add object key 
Javascript :: fs flies in dir 
Javascript :: how to use if else inside jsx in react 
Javascript :: jquery add br in text 
Javascript :: import in react js 
Javascript :: black adam 
Javascript :: sum of all elements in array javascript 
Javascript :: embedded javascript 
Javascript :: urlsearchparams to object 
Javascript :: open youtube video at specific time javascript 
Javascript :: using arrow function and destructuring 
Javascript :: bfs javascript 
Javascript :: js iife 
Javascript :: javascript array randomizer 
Javascript :: how to get current date in express js 
Javascript :: jquery window new tab with post 
Javascript :: extended class call method from super in javascript 
Javascript :: trim text after a certain word in js 
Javascript :: module export in node js 
Javascript :: mongoose check if user exists 
Javascript :: jquery append after number of chars in string 
Javascript :: get image src width and height 
Javascript :: node 10 form data 
Javascript :: js check if a string is a number 
Javascript :: js get img under div 
Javascript :: flatten json python 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =