Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a new object with only a subset of properties using vanilla Javascript
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
}Advertisements