Skip to content Skip to sidebar Skip to footer

How To Check Equality Of Two Objects(only Specific Key Values)

How to check equality of two objects(only specific key values) which is one object type is number other one comes as a string type, but they are numbers. I want to check equality o

Solution 1:

You can just compare if the two objects have the same keys using Object.keys.

function keysEqual(obj1, obj2){
    const keys1 = Object.keys(obj1), keys2 = Object.keys(obj2);
    return keys1.length === keys2.length && keys1.every(key => keys2.includes(key))
}
var object1 = {
        id: 1234,
        x1: "1.000000",
        x2: undefined,
        x3: "1.0",
        x4: "1.0",
        x5:  1
};
var object2 = {
        id: 3456,
        x1: 1,
        x2: undefined,
        x3: 1,
        x4: 1,
        x5:  2
};
console.log(keysEqual(object1, object2));

Solution 2:

This is a modified version of the accepted answer of your previous question - The two objects are only consider equal if fields "x1", "x2", "x3", "x4" are loosely equal - other fields are left out of the comparison step

The gist of the algorithm is to use Js every function: For every field in ["x1", "x2", "x3", "x4"], object1 must (loosely) equal object2 at that field

const fieldsToCheck = ["x1", "x2", "x3", "x4"];

function equal(obj1, obj2, fieldsToCheckFor = fieldsToCheck) {
  return fieldsToCheckFor.every((key) => obj1[key] == obj2[key]);
}

var object1 = {
  id: 1234,
  x1: "1.000000",
  x2: undefined,
  x3: "1.0",
  x4: "1.0",
};
var object2 = {
  id: 3456,
  x1: 1,
  x2: undefined,
  x3: 1,
  x4: 1,
};

var object3 = {
  id: 3456,
  x1: 1,
  x2: undefined,
  x3: 1,
  x4: 1,
  x5: 7,
  randomFieldThatDoesNotGetChecked: "a72",
};

var object4 = {
  id: 3456,
  x1: 1,
  x2: "x2 has been defined",
  x3: 1,
  x4: 1,
  x5: 7,
  randomFieldThatDoesNotGetChecked: "a72",
};

// Should be True
console.log("Test1", equal(object1, object2));

// Should be True
console.log("Test2", equal(object2, object3));

// Should be False
console.log("Test3", equal(object1, object4));

Solution 3:

var object1 = {
        id: 1234,
        x1: "1.000000",
        x2: undefined,
        x3: "1.0",
        x4: "1.0",
        x5: 1
};
var object2 = {
        id: 3456,
        x1: 1,
        x2: undefined,
        x3: 1,
        x4: 1,
        x5: 2
};

var check = ["x1", "x2", "x3", "x4"];

console.log(eval());

function eval() {
  for (var i = 0; i < check.length; i++) {
    if (object1[check[i]] != object2[check[i]]) return false;
  }
  return true;
}

Solution 4:

You can iterate through each key using array#every and check if key exists in both object and then compare their values.

const object1 = { id: 1234, x1: "1.000000", x2: undefined, x3: "1.0", x4: "1.0", x5: 'somevale 1' },
      object2 = { id: 3456, x1: 1, x2: undefined, x3: 1, x4: 1, x5: 'somevalue 2' },
      keys = ['x1', 'x2', 'x3', 'x4'],
      result = keys.every(key => key in object1 && key in object2 && object1[key] == object2[key]);
console.log(result);

Post a Comment for "How To Check Equality Of Two Objects(only Specific Key Values)"