主题
matches
js
_.matches(source)
创建一个函数,对给定对象和 source
进行部分深度比较,如果给定对象具有等效属性值,则返回 true
,否则返回 false
。
¥Creates a function that performs a partial deep comparison between a given object and source
, returning true
if the given object has equivalent property values, else false
.
注意:创建的函数相当于部分应用了 source
的 _.isMatch
。
¥Note: The created function is equivalent to _.isMatch
with source
partially applied.
部分比较将分别将空数组和空对象 source
值与任何数组或对象值进行匹配。有关支持的值比较的列表,请参阅 _.isEqual
。
¥Partial comparisons will match empty array and empty object source
values against any array or object value, respectively. See _.isEqual
for a list of supported value comparisons.
注意:可以通过使用 _.overSome
组合多个匹配器来检查多个值
¥Note: Multiple values can be checked by combining several matchers using _.overSome
新增于
¥Since
3.0.0
参数
¥Arguments
source
(对象):要匹配的属性值的对象。¥
source
(Object): The object of property values to match.
返回
¥Returns
(函数):返回新的 spec 函数。
¥(Function): Returns the new spec function.
示例
¥Example
js
var objects = [
{ 'a': 1, 'b': 2, 'c': 3 },
{ 'a': 4, 'b': 5, 'c': 6 }
];
_.filter(objects, _.matches({ 'a': 4, 'c': 6 }));
// => [{ 'a': 4, 'b': 5, 'c': 6 }]
// Checking for several possible values
_.filter(objects, _.overSome([_.matches({ 'a': 1 }), _.matches({ 'a': 4 })]));
// => [{ 'a': 1, 'b': 2, 'c': 3 }, { 'a': 4, 'b': 5, 'c': 6 }]