Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
Javascript remove object keys that are not in both objects with Lodash?

So, I have this two objects:

const obj1 = {
  foo: 'Foo',
  bar: {
    baz: 'Baz',
  }
}

const obj2 = {
  foo: 'Foo',
  bar: {
    baz: 'Baz',
    zip: 'Zip'
  },
}

I want to remove "zip" from obj2 since it's not in obj1. Is there a way to do this with lodash?

EDIT: If a property is an object, it will be an object in both of them. There can't be a string with the same key of an object in the other one.

The use case is, I have a translation file and after some processing, I need to remove the ones that are no longer needed. So if I delete them in the original object (i.e. obj1) they have to be removed from the other ones (in this case, obj2.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you are open to using additional libraries, I can recommend looking into deep-diff:

const obj1 = {
  foo: 'Foo',
  bar: {
    baz: 'Baz',
  }
}

const obj2 = {
  foo: 'Foo',
  bar: {
    baz: 'Baz',
    zip: 'Zip'
  },
}

DeepDiff.observableDiff(obj2, obj1, d => {
  DeepDiff.applyChange(obj2, obj1, d)
});

console.log(obj1)
console.log(obj2)
<script src="https://cdn.jsdelivr.net/npm/deep-diff@1/dist/deep-diff.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!