Sometimes, we want to omit specific properties from an object in JavaScript.
In this article, we’ll look at how to omit specific properties from an object in JavaScript.
How to omit specific properties from an object in JavaScript?
To omit specific properties from an object in JavaScript, we can create a function that returns an object with the properties we want to keep.
For instance, we write
const exampleFilter = ({ keepMe, keepMeToo }) => ({ keepMe, keepMeToo });
console.log(
exampleFilter({
keepMe: "keepMe",
keepMeToo: "keepMeToo",
omitMe: "omitMe",
omitMeToo: "omitMeToo",
})
);
to define the exampleFilter
function.
In it, we destructure the properties from the parameter object.
And then we return an object with the properties we destructured included,
Conclusion
To omit specific properties from an object in JavaScript, we can create a function that returns an object with the properties we want to keep.