Skip to content

Instantly share code, notes, and snippets.

@callezenwaka
Last active July 3, 2021 09:43
Show Gist options
  • Save callezenwaka/2b8cf2862784f360ae7545ce3bb4f8cf to your computer and use it in GitHub Desktop.
Save callezenwaka/2b8cf2862784f360ae7545ce3bb4f8cf to your computer and use it in GitHub Desktop.
How to filter out unwanted items from JavaScript object.
// 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