0

I have 2 objects a and b i want to compare only data of objects of same keys

var a = {name1:'alisha',age:18,subject:{main:'javascript',major:'maths'}};
var b = {name1:'alisha',age:'18'};

 function compareData(a,b){
  return JSON.stringify(a) == JSON.stringify(b); 
 }

 console.log(compareData(a,b))

This is comparing all the keys but i want to compare only available data of available keys how can I do this

Alisha Sharma
  • 139
  • 2
  • 15
  • So you want the result to be true if all the keys/values of `b` are in `a`? Or if there's at least one? Or something else? Maybe an intersection of keys and then compare their values? Really hard to tell what you want. –  Jan 16 '18 at 17:44
  • Iterate over the properties of one object, check whether it is contained in the other object with `hasOwnProperty` or `in` and compare. – Felix Kling Jan 16 '18 at 17:44
  • 1
    Note that `JSON.stringify` is not the right tool for this, as it may order the keys differently than you expect. – trincot Jan 16 '18 at 17:48
  • I want this to return return true because age and name properties have same data where properties might be not in proper order – Alisha Sharma Jan 16 '18 at 17:48
  • https://stackoverflow.com/questions/11846484/each-for-object – Aaron Franco Jan 16 '18 at 17:51
  • So what if there are properties in `b` that do not exist in `a`? Is that a possibility? Should it be `false` in that case or should it only compare the intersection of matching property names? –  Jan 16 '18 at 17:56

2 Answers2

2

You can use this for shallow comparison:

// Returns true if the values are equal (shallow)
// for all properties that both objects have
function compareData(a,b){
  return Object.keys(a).filter( k => k in b ).every( k => a[k] === b[k] );
}

It will return false for your objects because one has a number for age and the other has a string. It will return true for these objects:

var a = {name1:'alisha',age:18,subject:{main:'javascript',major:'maths'}};
var b = {name1:'alisha',age:18};

Example:

console.log( compareData( {}, {age: 5} ) ); // true (no shared properties)
console.log( compareData( {age: 5}, {age: 5} ) ); // true (equal shared properties)
console.log( compareData( {age: 4}, {age: 5} ) ); // false

var a = {name1:'alisha',age:18,subject:{main:'javascript',major:'maths'}};
var b = {name1:'alisha',age:18};

console.log( compareData( a, b ) ); // true

function compareData(a,b){
  return Object.keys(a).filter( k => k in b ).every( k => a[k] === b[k] );
}
Paul
  • 139,544
  • 27
  • 275
  • 264
1

Looping through the first item keys and checking if:
1. They exist
2. The value is the same

Seems to work:

var a = { name1:'alisha', age:18,subject:{main:'javascript',major:'maths'}};
var b = { name:'alisha', age: 19 };

function areTheSame(a,b){
  // fat arrow function returns true if any element has a difference
  return !Object.keys(a).some(key => {
    if(b.hasOwnProperty(key)){
      return a[key] !== b[key];
    }
    return false;
  });
}

console.log(areTheSame(a,b))
Antony
  • 1,253
  • 11
  • 19