Lodash _.without() 方法
_.without 方法以过滤形式创建一个新数组,其中包含要排除的值并提供新值作为输出。
注意:它与 _.pull() 方法不同,该方法返回一个新数组。
句法:
_.without(array, [values])
参数:此方法接受上面提到的两个参数,如下所述:
- array:此参数保存要检查的数组。
- [values]:此参数保存要排除的值。
返回值:此方法返回过滤值的新数组。
示例一:这里使用 const _ = require('lodash') 来导入文件中的 lodash 库。
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var w = _.without([3.2, 5.2,
5.2, 1.2], 5.2, 3.2);
// Use of _.without() method
let gfg = _.without(w);
// Printing the output
console.log(gfg);
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var w = _.without([3, 5, 5, 1], 1, 3);
var t = _.without([4, 7, 4, 8], 7, 4);
// Use of _.without() method
let gfg = _.without(w);
let gfg1 = _.without(t);
// Printing the output
console.log(gfg);
console.log(gfg1);
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var w = _.without(['a', 'b',
'c', 'b' ], 'a', 'b');
var x = _.without(['C++', 'Java',
'Python', '.Net' ], 'C++', 'Java');
// Use of _.without() method
let gfg = _.without(w);
let gfg2 = _.without(x);
// Printing the output
console.log(gfg);
console.log(gfg2);
输出:
[ 1.2 ]
示例 2:
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var w = _.without([3, 5, 5, 1], 1, 3);
var t = _.without([4, 7, 4, 8], 7, 4);
// Use of _.without() method
let gfg = _.without(w);
let gfg1 = _.without(t);
// Printing the output
console.log(gfg);
console.log(gfg1);
输出:
[ 5, 5 ]
[ 8 ]
示例 3:
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var w = _.without(['a', 'b',
'c', 'b' ], 'a', 'b');
var x = _.without(['C++', 'Java',
'Python', '.Net' ], 'C++', 'Java');
// Use of _.without() method
let gfg = _.without(w);
let gfg2 = _.without(x);
// Printing the output
console.log(gfg);
console.log(gfg2);
输出:
[ 'c' ]
[ 'Python', '.Net' ]
注意:此代码在普通 JavaScript 中不起作用,因为它需要安装库 lodash。