Omit matching object keys

JavaScript, Object · Oct 21, 2020

Creates an object composed of the properties the given function returns falsy for.

const omitBy = (obj, fn) =>
  Object.keys(obj)
    .filter(k => !fn(obj[k], k))
    .reduce((acc, key) => ((acc[key] = obj[key]), acc), {});

omitBy({ a: 1, b: '2', c: 3 }, x => typeof x === 'number'); // { b: '2' }

More like this

  • JavaScript Object Key Selection

    Pick and omit keys from JavaScript objects easily, using the snippets in this collection.

    Collection · 4 snippets

  • Pick matching object keys

    Creates an object composed of the properties the given function returns truthy for.

    JavaScript, Object · Oct 22, 2020

  • Find matching keys

    Finds all the keys in the provided object that match the given value.

    JavaScript, Object · Nov 15, 2020

  • Find first matching key

    Finds the first key that satisfies the provided testing function. Otherwise undefined is returned.

    JavaScript, Object · Oct 22, 2020