_.dropRightWhile(array, [predicate=_.identity])
创建一个切片数组,去除array中从结尾开始到 predicate 返回假值结束部分。predicate 会传入3个参数: (value, index, array)。
var users = [
{ 'user': 'barney', 'active': true },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': false }
];
_.dropRightWhile(users, function(o) { return !o.active; });
// => objects for ['barney']
// The `_.matches` iteratee shorthand.
_.dropRightWhile(users, { 'user': 'pebbles', 'active': false });
// => objects for ['barney', 'fred']
// The `_.matchesProperty` iteratee shorthand.
_.dropRightWhile(users, ['active', false]);
// => objects for ['barney']
// The `_.property` iteratee shorthand.
_.dropRightWhile(users, 'active');
// => objects for ['barney', 'fred', 'pebbles']
解析:经过看官方的列子,一脸萌比,主要是官方的注释太迷惑(objects for),后来自己执了下结果后官方提供的结果不同,使用版本为lodash@4.17.11.
下面的例子结果应该更好理解一下,只是从数组的后面开始进行判断,知道返回false结束。
var users = [
{ 'user': 'barney', 'active': true },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': false }
];
_.dropRightWhile(users, function(o) { return !o.active; });
[ { user: 'barney', active: true } ]
// The `_.matches` iteratee shorthand.
_.dropRightWhile(users, { 'user': 'pebbles', 'active': false });
// => [ { user: 'barney', active: true },{ user: 'fred', active: false } ]
// The `_.matchesProperty` iteratee shorthand.
_.dropRightWhile(users, ['active', false]);
// => [ { user: 'barney', active: true } ]
// The `_.property` iteratee shorthand.
_.dropRightWhile(users, 'active');
//[ { user: 'barney', active: true },{ user: 'fred', active: false },{ user: 'pebbles', active: false } ]