Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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

jsdoc object destructuring

/**
 * My cool function.
 *
 * @param {Object} obj - An object.
 * @param {string} obj.prop1 - Property 1.
 * @param {string} obj.prop2 - Property 2.
 */
const fn = function ({prop1, prop2}) {
  // Do something with prop1 and prop2
}
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

JSDoc doucumenting destructured object parameter

/**
 * My cool function.
 *
 * @param {Object} obj - An object.
 * @param {string} obj.prop1 - Property 1.
 * @param {string} obj.prop2 - Property 2.
 */
const fn = function ({prop1, prop2}) {
  // Do something with prop1 and prop2
}
Comment

destructuring an object in JS

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

PREVIOUS NEXT
Code Example
Javascript :: javascript substration between times 
Javascript :: pub js npm 
Javascript :: java script or js circle collision 
Javascript :: delete method 
Javascript :: promise javascript 
Javascript :: mobile nav react npm 
Javascript :: save item in array javascript 
Javascript :: basics of switch case and if else 
Javascript :: create owl component react js 
Javascript :: get unique id angular 
Javascript :: regex number 
Javascript :: NextJS add lang attribute to HTML tag 
Javascript :: how to craete an array in js 
Javascript :: what is package.json 
Javascript :: invertir un array javascript 
Javascript :: javascript save as pdf 
Javascript :: function javascript 
Javascript :: react without using jsx create element 
Javascript :: angular2-tree-diagram 
Javascript :: Example of Reactjs Controlled-Components 
Javascript :: olx clone react 
Javascript :: nodejs mysql transactions 
Javascript :: React Hook "useState" is called in function "app" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks 
Javascript :: how to target two id using jquery 
Javascript :: why is this undefined in react 
Javascript :: react map array 
Javascript :: nested for loop in angular 
Javascript :: This function is used to store items in local storage 
Javascript :: DateRangePicker start and end date in one Textfeild material ui 
Javascript :: how to add icon in javascript 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =