Files
30-seconds-of-code/snippets/omit.md
Isabelle Viktoria Maciohsek 8bf67754ce Make expertise a field
2022-03-01 20:21:45 +02:00

682 B

title, tags, expertise, firstSeen, lastUpdated
title tags expertise firstSeen lastUpdated
Omit object properties object intermediate 2018-01-19T13:14:46+02:00 2020-10-21T21:54:53+03:00

Omits the key-value pairs corresponding to the given keys from an object.

  • Use Object.keys(), Array.prototype.filter() and Array.prototype.includes() to remove the provided keys.
  • Use Array.prototype.reduce() to convert the filtered keys back to an object with the corresponding key-value pairs.
const omit = (obj, arr) =>
  Object.keys(obj)
    .filter(k => !arr.includes(k))
    .reduce((acc, key) => ((acc[key] = obj[key]), acc), {});
omit({ a: 1, b: '2', c: 3 }, ['b']); // { 'a': 1, 'c': 3 }