DEV Community

Cover image for How to Select or Omit Properties From an Object in JavaScript
Nick Scialli (he/him)
Nick Scialli (he/him)

Posted on • Originally published at typeofnan.dev

19 6

How to Select or Omit Properties From an Object in JavaScript

Selecting or omitting properties from a JavaScript object is a fairly common problem without a built-in solution. In this post, we're going to roll our own pick and omit utility functions to help us accomplish these goals.


If you enjoy this tutorial, please give it a 💓, 🦄, or 🔖 and consider:

📬 signing up for my free weekly dev newsletter
🎥 subscribing to my free YouTube dev channel


Selecting Properties from an Object

If we want to select any number of properties from a JavaScript object, we can implement the following pick function:



function pick(obj, ...props) {
  return props.reduce(function(result, prop) {
    result[prop] = obj[prop];
    return result;
  }, {});
}


Enter fullscreen mode Exit fullscreen mode

Let's see this in action! Our first argument to the pick function will be the object we want to pick from and the subsequent arguments will the the names of the keys we want to keep.



const person = {
  name: 'Pete',
  dog: 'Daffodil',
  cat: 'Omar',
};

const dogPerson = pick(person, 'name', 'dog');

console.log(dogPerson);
// { name: "Pete", dog: "Daffodil" }


Enter fullscreen mode Exit fullscreen mode

We see that by providing the person object as the first argument and then the strings "name" and "dog" as the subsequent arguments, we're able to retain the "name" and "dog" props from our object while disregarding the "cat" prop.

Omitting Properties From an Object

If we want to omit any number of properties from a JavaScript object, we can implement the following omit function:



function omit(obj, ...props) {
  const result = { ...obj };
  props.forEach(function(prop) {
    delete result[prop];
  });
  return result;
}


Enter fullscreen mode Exit fullscreen mode

Again, let's use the same person object to see this in action.



const person = {
  name: 'Pete',
  dog: 'Daffodil',
  cat: 'Omar',
};

const catPerson = omit(person, 'dog');

console.log(catPerson);
// { name: "Pete", cat: "Omar" }


Enter fullscreen mode Exit fullscreen mode

We can see that, by providing our person object as the first argument and the string "dog" as the second argument, we're able to get a new object with the "dog" property omitted!brienne-hong-xEPMg4qfjOs-unsplash (1)

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Another way would be:

const filterObj = (obj, f) => (o=Object).fromEntries(o.entries(obj).filter([k,v]=>f(k)))
const select = (obj, ...props) => filterObj(obj, k => props.includes(k))
const omit = (obj, ...props) => filterObj(obj, k => !props.includes(k))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ke9414 profile image
Kevin Amador

Awesome, this example is useful. Thanks a lot :)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay