In this post, we are going to learn about How to get subset of object in JavaScript with examples. There are many ways in Javascript and ES6 to get specific property from object. We will learn them with techniques like destructing assignment,foreach loop, rest operator, assign(), reduce() method in javascript with each program.
1. destructing assignment to get subset of object in JavaScript
In this example We are using destructing assignment to get the specific properties Empname and Dep from Employee object.destructing assigment to unpack object into bunch of varaibles.It is easy used to create a subset of objects.
let Employee = {Empname: 'Jack', salary: 60000,EmpID:1,Dep:'Admin'}
const accessobj = (({ Empname, salary }) => ({ Empname, salary }))(Employee);
console.log(accessobj)
Output
{ Empname: 'Jack', salary: 60000 }
2. Assign properties to get subset of object in JavaScript
To get the subset of javascript object we have to create a new object EmpData in which partaily intialized specific properties from the original object Employee.
let Employee = {Empname: 'Jack', salary: 60000,EmpID:1,Dep:'Admin'}
var EmpData = {Empname: Employee.Empname, salary: Employee.salary};
console.log(EmpData);
Output
{ Empname: 'Jack', salary: 60000 }
3. Reduce() to Get specific property of object in JavaScript
In this javascript program, we have used reduce() to get subset of original object Employee.The array.reduce() method call a callback function for each element of the array and the result returned by the callback function after each iteration pass to the accumulator of reduce function that is used for the next iteration.
let Employee = {Empname: 'Jack', salary: 60000,EmpID:1,Dep:'Admin'}
var EmpData = ['Empname','salary'].reduce((acc, key) => { acc[key] = Employee[key]; return acc; }, {});
console.log(EmpData);
Output
{ Empname: 'Jack', salary: 60000 }
4. Foreach loop to get subset of object in JavaScript
in this javascript example we have defined a function seleprop(obj, …props )that accept original object and properties that we want to delete from object with the help of rest parameter. Inside the loop foreach loop will iterate over all properties and delete them using the delete operator and return a subset of Employee object
let Employee = {Empname: 'Jack', salary: 60000,EmpID:1,Dep:'Admin'}
function seleprop(obj, ...props)
{
props.forEach(function (props)
{
delete obj[props];
});
return obj;
}
console.log(seleprop(Employee, 'EmpID','Dep'))
Output
{ Empname: 'Jack', salary: 60000 }
5. filter() and map() to get subset of object in JavaScript
In this jaavscript program we have defined a function selectdata that accepet object and keys as paremeter and retrun a subset of original object Employee.
let Employee = {Empname: 'Jack', salary: 60000,EmpID:1,Dep:'Admin'}
const selectdata = (obj, ...keys) => Object.fromEntries(
keys
.filter(key => key in obj)
.map(key => [key, obj[key]])
);
console.log(selectdata (Employee,'Empname','salary'))
Output
{ Empname: 'Jack', salary: 60000 }
6. assign() and map() to get subset of object in JavaScript
iIn this example we have used assign() along with map() function to get the subset of original object.The assign() method is used to copy all properties from source to target object in javascript. Here we have used to copy the subset of original object to EmpData object.
let Employee = {Empname: 'Jack', salary: 60000,EmpID:1,Dep:'Admin'}
const keys = ['Empname', 'salary']
Empdata = (obj, keys) => {
return Object.assign({}, ...keys.map(key => ({
[key]: obj[key]
})))
}
console.log(Empdata(Employee,keys))
Output
{ Empname: 'Jack', salary: 60000 }
7. Filter() and includes() to get subset of object in JavaScript
In this example we have an array of keys const keys = [‘Empname’, ‘salary’] that we want to selet from original object.We have used object.entries() method to get a separate array of keys and values and array.fillter() to filter the given key from object Employee.Let us understand with below code.
let Employee = {Empname: 'Jack', salary: 60000,EmpID:1,Dep:'Admin'}
const keys = ['Empname', 'salary']
Empdata = Object.fromEntries(
Object.entries(Employee)
.filter(([key]) => keys.includes(key))
);
console.log(Empdata)
Output
{ Empname: 'Jack', salary: 60000 }
Summary
In this post we have learned mutiple ways of How to get subset of object in JavaScript with code example by using follwing technique destructing assignment,foreach loop, rest operator,assign(),reduce() method in jaavscript each programs.