Last active
July 3, 2021 09:43
-
-
Save callezenwaka/2b8cf2862784f360ae7545ce3bb4f8cf to your computer and use it in GitHub Desktop.
How to filter out unwanted items from JavaScript object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// filter out unnecessary key-value pairs | |
const omit = (obj, arr) => | |
Object.keys(obj) | |
.filter(k => !arr.includes(k)) | |
.reduce((acc, key) => ((acc[key] = obj[key]), acc), {}); | |
let item = {name: 'John Doe', profession: 'Developer', age: 30, gender: 'male', height: '1.6m', weight: '80kg', country: 'UK'}; | |
let filteredItem = omit(item, ['gender', 'height', 'weight', 'country']); | |
// console.log(filteredItem); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment