现在我有这个功能:
function without(array, index) {
array.splice(index, 1);
return array;
}
我认为这将是lodash会有一个实用工具,但看起来不是。
有了这个功能,我可以做一个我可以连接的单线:
var guestList = without(guests, bannedGuest).concat(moreNames);
使用lodash无法在不引入谓词功能的情况下实现此?
答案 0 :(得分:1)
_.without
已经存在。或者,您也可以使用_.pull
,这会改变给定的参数数组。
var guests = ['Peter', 'Lua', 'Elly', 'Scruath of the 5th sector'];
var bannedGuest = 'Scruath of the 5th sector';
var bannedGuests = ['Peter', 'Scruath of the 5th sector'];
console.debug(_.without(guests, bannedGuest )); // ["Peter", "Lua", "Elly"]
不直接支持禁止一组访客,但我们可以轻松解决这个问题:
// banning an array of guests is not yet supported, but we can use JS apply:
var guestList = _.without.apply(null, [guests].concat(bannedGuests));
console.debug(guestList); // ["Lua", "Elly"]
// or if you are feeling fancy or just want to learn a new titbit, we can use spread:
guestList = _.spread(_.without)([guests].concat(bannedGuests));
console.debug(guestList); // ["Lua", "Elly"]