underscorejs-where学习

2.7 where

2.7.1 语法:

_.where(list, predicate)

2.7.2 说明:

对list集合的每个对象依次与predicate对象进行匹配,返回一个数组(数组为匹配成功的对象)

  • predicate是一个对象

2.7.3 代码示例:

示例一:where对数组,arguments进行操作,与predicate对象进行匹配(数组内需为对象)

//操作数组
_.where([{x: 1, y: 2}, {z: 3}], {x: 1});
//=> [{x: 1, y: 2}]

//操作arguments
function abc() {
    return  _.where(arguments, {z: 3});
}
abc({x: 1, y: 2}, {z: 3}); //=> [{z: 3}]

示例二:predicate需要一个对象否则直接返回list集合

_.where([{x: 1, y: 2}, {z: 3}]); //=> [{x: 1, y: 2}, {z: 3}]

_.where([{x: 1, y: 2}, {z: 3}], null); //=> [{x: 1, y: 2}, {z: 3}]

_.where('123'); //=> ["1", "2", "3"]

gitbook地址:https://www.gitbook.com/book/niec-fe/underscorejs/details

原文地址:https://www.cnblogs.com/kyo4311/p/5162413.html