主题
matchesProperty
js
_.matchesProperty(path, srcValue)创建一个函数,对给定对象的 path 值和 srcValue 进行部分深度比较,如果对象值等效,则返回 true,否则返回 false。
¥Creates a function that performs a partial deep comparison between the value at path of a given object to srcValue, returning true if the object value is equivalent, else false.
注意:部分比较将分别将空数组和空对象 srcValue 值与任何数组或对象值进行匹配。有关支持的值比较的列表,请参阅 _.isEqual。
¥Note: Partial comparisons will match empty array and empty object srcValue 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.2.0
参数
¥Arguments
path(数组|字符串):要获取的属性的路径。¥
path(Array|string): The path of the property to get.srcValue(*):要匹配的值。¥
srcValue()*: The value 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 }
];
_.find(objects, _.matchesProperty('a', 4));
// => { 'a': 4, 'b': 5, 'c': 6 }
// Checking for several possible values
_.filter(objects, _.overSome([_.matchesProperty('a', 1), _.matchesProperty('a', 4)]));
// => [{ 'a': 1, 'b': 2, 'c': 3 }, { 'a': 4, 'b': 5, 'c': 6 }]