Lodash _.reject() 方法
Lodash 是一个基于 underscore.js 的 JavaScript 库。 Lodash 有助于处理数组、集合、字符串、对象、数字等。
_.reject() 方法与 _.filter() 方法相反,该方法返回集合中谓词不返回 true 的元素。
句法:
_.reject(collection, predicate)
参数:此方法接受上面提到的两个参数,如下所述:
- 集合:此参数保存要迭代的集合。
- iteratee:此参数保存每次迭代调用的函数。
返回值:该方法用于返回新过滤的数组。
示例一:这里使用 const _ = require('lodash') 来导入文件中的 lodash 库。
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var users = [
{ 'user': 'Rohit', 'age': 25, 'active': false },
{ 'user': 'Mohit', 'age': 26, 'active': true } ];
// Use of _.reject() method
let gfg = _.reject(users, function(o)
{ return !o.active; });
// Printing the output
console.log(gfg);
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var users = [
{ 'employee': 'Rohit',
'salary': 50000,
'active': false
},
{ 'employee': 'Mohit',
'salary': 55000,
'active': true
}
];
// Use of _.reject() method
// The `_.matches` iteratee shorthand
let gfg = _.reject(users,
{ 'salary': 55000, 'active': true });
// Printing the output
console.log(gfg);
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var users = [
{ 'employee': 'Rohit',
'salary': 50000,
'active': false
},
{ 'employee': 'Mohit',
'salary': 55000,
'active': true
}
];
// Use of _.reject() method
// The `_.matchesProperty` iteratee shorthand
let gfg = _.reject(users, ['active', false]);
// Printing the output
console.log(gfg);
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var users = [
{ 'employee': 'Rohit',
'salary': 50000,
'active': false
},
{ 'employee': 'Mohit',
'salary': 55000,
'active': true
}
];
// Use of _.reject() method
// The `_.property` iteratee shorthand
let gfg = _.reject(users, 'active');
// Printing the output
console.log(gfg);
输出:
[ { user: 'Mohit', age: 26, active: true } ]
示例 2:
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var users = [
{ 'employee': 'Rohit',
'salary': 50000,
'active': false
},
{ 'employee': 'Mohit',
'salary': 55000,
'active': true
}
];
// Use of _.reject() method
// The `_.matches` iteratee shorthand
let gfg = _.reject(users,
{ 'salary': 55000, 'active': true });
// Printing the output
console.log(gfg);
输出:
[ { employee: Rohit, salary: 50000, active: false } ]
示例 3:
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var users = [
{ 'employee': 'Rohit',
'salary': 50000,
'active': false
},
{ 'employee': 'Mohit',
'salary': 55000,
'active': true
}
];
// Use of _.reject() method
// The `_.matchesProperty` iteratee shorthand
let gfg = _.reject(users, ['active', false]);
// Printing the output
console.log(gfg);
输出:
[ { employee: Mohit, salary: 55000, active: true } ]
示例 4:
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var users = [
{ 'employee': 'Rohit',
'salary': 50000,
'active': false
},
{ 'employee': 'Mohit',
'salary': 55000,
'active': true
}
];
// Use of _.reject() method
// The `_.property` iteratee shorthand
let gfg = _.reject(users, 'active');
// Printing the output
console.log(gfg);
输出:
[ { employee: Rohit, salary: 50000, active: false } ]
注意:此代码在普通 JavaScript 中不起作用,因为它需要安装库 lodash。