How to get a subset of a javascript object's properties?

JavascriptFront End TechnologyObject Oriented Programming

To get a subset of object's properties and create a new object out of those properties, use object destructuring and property shorthand. For example, You have the following object −

Example

const person = {
   name: 'John',
   age: 40,
   city: 'LA',
   school: 'High School'
}

And you only want the name and age, you can create the new objects using −

const {name, age} = person;
const selectedObj = {name, age};
console.log(selectedObj);

Output

This will give the output −

{
   name: 'John',
   age: 40
}
raja
Published on 02-Dec-2019 05:52:41
Advertisements