Lodash _.takeRightWhile() 方法
_.takeRightWhile 方法用于创建数组的切片,其中从末尾获取元素并获取这些元素,直到谓词错误返回。
句法:
_.takeRightWhile(array, [predicate=_.identity])
参数:此方法接受上面提到的两个参数,如下所述:
- array:此参数保存要查询的数组。
- [predicate=_.identity]:此参数保存每次迭代调用的函数。
返回值:该方法用于返回数组的切片。
示例一:这里使用 const _ = require('lodash') 来导入文件中的 lodash 库。
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var users = [{ 'user': 'fred', 'active': false
},
{ 'user': 'pebbles', 'active': false }];
// Use of _.takeRightWhile()
// method
let ind = _.takeRightWhile(users, function(o) {
return !o.active; });
// Printing the output
console.log(ind);
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var users = [{ 'user': 'fred', 'active': false
},
{ 'user': 'pebbles', 'active': false }];
// Use of _.takeRightWhile()
// method
// The `_.matches` iteratee shorthand.
let gfg = _.takeRightWhile(users, { 'user':
'pebbles', 'active': false });
// Printing the output
console.log(gfg);
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var users = [{ 'user': 'fred', 'active': false
},
{ 'user': 'pebbles', 'active': false }];
// Use of _.takeRightWhile()
// method
// The `_.matchesProperty` iteratee shorthand.
let gfg = _.takeRightWhile(users, ['active',
false]);
// Printing the output
console.log(gfg);
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var users = [{ 'user': 'fred', 'active': false
},
{ 'user': 'pebbles', 'active': false }];
// Use of _.takeRightWhile()
// method
// The `_.property` iteratee shorthand.
let gfg =_.takeRightWhile(users, 'active');
// Printing the output
console.log(gfg);
输出:
[{ user: 'fred', active: false }, {user: 'pebbles', active: false}]
示例 2:
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var users = [{ 'user': 'fred', 'active': false
},
{ 'user': 'pebbles', 'active': false }];
// Use of _.takeRightWhile()
// method
// The `_.matches` iteratee shorthand.
let gfg = _.takeRightWhile(users, { 'user':
'pebbles', 'active': false });
// Printing the output
console.log(gfg);
输出:
[{user: 'pebbles', active: false}]
示例 3:
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var users = [{ 'user': 'fred', 'active': false
},
{ 'user': 'pebbles', 'active': false }];
// Use of _.takeRightWhile()
// method
// The `_.matchesProperty` iteratee shorthand.
let gfg = _.takeRightWhile(users, ['active',
false]);
// Printing the output
console.log(gfg);
输出:
[{ user: 'fred', active: false }, {user: 'pebbles', active: false}]
示例 4:
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var users = [{ 'user': 'fred', 'active': false
},
{ 'user': 'pebbles', 'active': false }];
// Use of _.takeRightWhile()
// method
// The `_.property` iteratee shorthand.
let gfg =_.takeRightWhile(users, 'active');
// Printing the output
console.log(gfg);
输出:
[]
注意:此代码在普通 JavaScript 中不起作用,因为它需要安装库 lodash。