0

If I have two objects:

const objA = { name: 'Tom', age: '20', location: 'London' };
const objB = { name: 'Jimmy', age: '35', occupation: 'Welder', hobbies: ['Football'] }

How can I get only the values that are only present in objA and not in objB, I want the result to be:

{
    location: 'London'
}

Is there a method in Lodash for that?

Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
Keselme
  • 3,779
  • 7
  • 36
  • 68
  • 1
    Combining `Object.keys` and https://lodash.com/docs/4.17.15#difference looks like it might work? – evolutionxbox Sep 15 '22 at 11:43
  • 2
    Maybe this help you. Check this [answer](https://stackoverflow.com/questions/57669696/getting-difference-object-from-two-objects-using-es6) – RomaZakharov Sep 15 '22 at 11:46
  • @evolutionxbox Thanks. Is there something for objects and not arrays? `difference` will return the different keys indeed, but I'll need to construct the new object myself and I want to avoid this – Keselme Sep 15 '22 at 11:46
  • `How can I get only the values that are only present in objA and not in objB` you mean **keys**? – GrafiCode Sep 15 '22 at 11:50
  • 1
    @GrafiCode: The output looks more like the key-value pair(s). – Lain Sep 15 '22 at 11:51
  • 1
    @GrafiCode no, I mean key-value pairs. – Keselme Sep 15 '22 at 11:53

3 Answers3

2

You can use _.pickBy() and _.has() to get the desired result using lodash.

We'll use the predicate: (value, key) => !_.has(objB, key) to return entries only present in objA.

const objA = { name: 'Tom', age: '20', location: 'London' };
const objB = { name: 'Jimmy', age: '35', occupation: 'Welder', hobbies: ['Football'] }

const difference = _.pickBy(objA, (value, key) => !_.has(objB, key));
console.log('Difference:', difference)
   
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

You can also do this in vanilla JavaScript using Object.entries(), Object.fromEntries() and Array.filter():

const objA = { name: 'Tom', age: '20', location: 'London' };
const objB = { name: 'Jimmy', age: '35', occupation: 'Welder', hobbies: ['Football'] }

const difference = Object.fromEntries(
    Object.entries(objA).filter(([key, value]) => {
        return !objB.hasOwnProperty(key);
    })
);

console.log('Difference:', difference)
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
2

Native, modern JS only:

const objA = { name: 'Tom', age: '20', location: 'London' };
const objB = { name: 'Jimmy', age: '35', occupation: 'Welder', hobbies: ['Football'] };

const diff = Object.fromEntries(Object.entries(objA).filter(([k, v]) => !Object.hasOwn(objB, k)));

console.log(diff);

Please note that this solution makes use of several relatively new additions to JavaScript:

connexo
  • 53,704
  • 14
  • 91
  • 128
0

Have you ever tried something like this?

for (key in objA) {
  if (!objB[key]) {
    console.log(key, objA[key]);
  }
}

or something like:

let newObj = {};

for (key in objA) {
  if (!objB[key]) {
    newObj[key] = objA[key];
  }
}

console.log(newObj);
  • Better to use `Object.hasOwnProperty`. That way you can also find falsy object values and exclude attributes defined in the prototype. – mousetail Sep 15 '22 at 11:59