0

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 of x1, x2, x3 and x4 value of the two objects not all of the object values

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

I received the answer from How to check equality of two objects, one object's attribute's type is number other ones type is string but they are numbers This solution checks every attributes of the object which I get false for my two objects, because other attributes can not be same.

Chamie
  • 15
  • 1
  • 5

4 Answers4

0

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));
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

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));
Bao Huynh Lam
  • 974
  • 4
  • 12
  • Object 1's x1, x2, x3 and x4 values are string type but they are numbers Object 2's x1, x2, x3 and x4 values are number type I want to check equality of object 1's(x1, x2, x3 and x4) against Object 2's x1, x2, x3 and x4 whether Object 1(x1, x2, x3 and x4) === Object 2 (x1, x2, x3 and x4) – Chamie Jul 12 '21 at 01:48
  • I think that's exactly what I did (or tried to do). Did anything not work for you in the code above ? – Bao Huynh Lam Jul 12 '21 at 04:03
0

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;
}
AlexSp3
  • 2,201
  • 2
  • 7
  • 24
0

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);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51