Search code examples
javascriptlodash

Is there a lodash function to merge two objects and delete properties of one of them if they don't exist in the other?


Consider the following two objects:

const source = {
  foo: 'value',
  bar: 'value',
  baz: 'value'
};

const pattern = {
  foo: '',
  bar: ''
};

_.fn(source, pattern); // { foo: 'value', bar: 'value' }

In this example 'baz' property is deleted because it doesn't exist in the pattern.


Solution

  • _.pick can help

    _.pick(source,Object.keys(pattern))