Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

object destructuring into this

const demo = { nextUrl: 'nextUrl', posts: 'posts' };

const target = {}; // replace target with this

({ nextUrl: target.nextUrl, posts: target.communityPosts } = demo);

console.log(target);
Comment

Destructuring of object in ES6

function printBasicInfo({firstName, secondName, profession}) {
	console.log(firstName + ' ' + secondName + ' - ' + profession)
}

var person = {
  firstName: 'John',
  secondName: 'Smith',
  age: 33,
  children: 3,
  profession: 'teacher'
}

printBasicInfo(person)
Comment

destructuring object

const obj = {
  name: "Fred",
  age: 42,
  id: 1
}

//simple destructuring
const { name } = obj;
console.log("name", name);

//assigning multiple variables at one time
const { age, id } = obj;
console.log("age", age);
console.log("id", id);

//using different names for the properties
const { name: personName } = obj;
console.log("personName", personName);
 Run code snippet
Comment

object destructuring example

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

// Object destructuring:
const { realName, address: { city } } = hero;
city; // => 'Gotham'
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

destructure to object

({x: oof.x, y: oof.y} = foo);
Comment

PREVIOUS NEXT
Code Example
Javascript :: search and delete instances of node_modules in directory 
Javascript :: how to move an array over one 
Javascript :: js chai setup 
Javascript :: web3 js connect to ropsten 
Javascript :: remove or add class jquery 
Javascript :: javascript array get element by index 
Javascript :: remove duplicates by id 
Javascript :: reprompt for permissions with getUserMedia() after initial denial 
Javascript :: convert number to words javascript lakh 
Javascript :: GET VISITOR IP ADDRESS USING JAVASCRIPT 
Javascript :: reinitialise or reset all values in mapping in solidity 
Javascript :: creat checkbox and append it to a list js 
Javascript :: Detect when the BACKSPACE is pressed 
Javascript :: parse thymeleaf variable onclick 
Javascript :: How to assign set a function as Variable 
Javascript :: redux how does dispatch know which reducer to call 
Javascript :: how to pass data to ejs partials 
Javascript :: javascript id generator 
Javascript :: jquery scroll to top of element 
Javascript :: Falsy Bouncer 
Javascript :: build function component react 
Javascript :: jquery code to javascript converter 
Javascript :: network information api js 
Javascript :: how to get current row value by clicking a button 
Javascript :: firebase messaging service not working with electron 
Javascript :: convert javascript to typescript online converter 
Javascript :: trigger many url calls JS 
Javascript :: cercle progress bar angular 
Javascript :: custu, loading next js 
Javascript :: how to check null and undefined 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =