如何实现类似lodash的对象浅层比较?

How to implement lodash-like shallow comparison of objects?

_.find(collection, [predicate=_.identity], [fromIndex=0]) 使用 predicate=_.identity 来匹配集合中的对象,例如

> _.find([{a: 'A', n: 0}, {a: 'A', n: 0}], {a: 'A'})
< Object {a: "A", n: 0}

用于执行比较的实际函数是什么?

例如我想检查特定对象(例如 {a: 'A', n: 0})是否与我的谓词 {a: 'A'}.

匹配

一个简单的解决方法是创建一个辅助数组并使用它来编写条件,例如!!_.find([{a: 'A', n: 0}], {a: 'A'})。虽然必须有一个函数来执行等价比较。

当你正在寻找方法

check if specific object (e.g. {a: 'A', n: 0}) matches my predicate {a: 'A'}

或许你可以使用 lodash 的 _.isMatch method.

> _.isMatch({a: 'A', n: 0}, {a: 'A'})
true