最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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']));

发布评论

评论列表(0)

  1. 暂无评论