jestjs - Javascript- Compare 2 object excluding certain keys - Stack Overflow

I'm creating 2 objects based on given values in my jest test and expect that they would be equal e

I'm creating 2 objects based on given values in my jest test and expect that they would be equal except of the property uuid which is generated indevidualy for each object.

uuid can be deeply nested multiple times. for example:

const object1 = { uuid: '5435443', name: 'xxx', branches: [{ uuid: '643643', children: [{ uuid: '65654' /* and so on */ }] }];
const object2 = { uuid: '7657657', name: 'xxx', branches: [{ uuid: '443444', children: [{ uuid: '09809' }] }];

How can I pare the objects, ignoring uuid property?

I'm creating 2 objects based on given values in my jest test and expect that they would be equal except of the property uuid which is generated indevidualy for each object.

uuid can be deeply nested multiple times. for example:

const object1 = { uuid: '5435443', name: 'xxx', branches: [{ uuid: '643643', children: [{ uuid: '65654' /* and so on */ }] }];
const object2 = { uuid: '7657657', name: 'xxx', branches: [{ uuid: '443444', children: [{ uuid: '09809' }] }];

How can I pare the objects, ignoring uuid property?

Share Improve this question edited Aug 23, 2021 at 10:20 fullstack asked Aug 23, 2021 at 10:05 fullstackfullstack 8447 silver badges22 bronze badges 1
  • You can omit keys with _.omit and deep pare objects with _.isEqual – jabaa Commented Aug 23, 2021 at 10:11
Add a ment  | 

3 Answers 3

Reset to default 3

You can remove uuid first than pare them.

const object1 = { uuid: '5435443', name: 'xxx', branches: [{ uuid: '643643', children: [{ uuid: '65654' /* and so on */ }] }]};
const object2 = { uuid: '7657657', name: 'xxx', branches: [{ uuid: '443444', children: [{ uuid: '09809' }] }]};

const removeUuid = o => {
  if (o) {
    switch (typeof o) {
    case "object":
      delete o.uuid;
      Object.keys(o).forEach(k => removeUuid(o[k]));
      break;
    case "array":
      o.forEach(a => removeUuid(a));
    }
  }
}

removeUuid(object1);
removeUuid(object2);

expect(object1).toBe(object2);

                                

You can use expect.any to ignore uuid attribute. Just make sure that the attributes are existing, and the uuid is a string:

describe("Compare 2 objects", () => {
  it("should ...", () => {
    const actual = { uuid: '5435443', name: 'xxx', branches: [{ uuid: '643643', children: [{ uuid: '65654' }] }] };

    const expected = {
      uuid: expect.any(String), // <--
      name: 'xxx',
      branches: [{ uuid: expect.any(String), children: [{ uuid: expect.any(String) }] }]
    };

    expect(actual).toEqual(expected);
  });
});

I succeeded to achieve this with the following solution:

const object1 = { uuid: '5435443', name: 'xxx', branches: [{ uuid: '643643', children: [{ uuid: '65654' /* and so on */ }] }] };
const object2 = { uuid: '7657657', name: 'xxx', branches: [{ uuid: '443444', children: [{ uuid: '09809' }] }] };

const pareExcludeKeys = (object1, object2, excludeKeys = []) => {
  if (Object.keys(object1).length !== Object.keys(object2).length) return false;
  return Object.entries(object1).reduce((isEqual, [key, value]) => {
    const isValueEqual = typeof value === 'object' && value !== null
      ? pareExcludeKeys(value, object2[key], excludeKeys) 
      : excludeKeys.includes(key) || object2[key] === value;
    return isEqual && isValueEqual;
  }, true);
};

console.log(pareExcludeKeys(object1, object2, ['uuid']));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745366526a4624605.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信