I am looking for an efficient way to remove all elements from a javascript array if they are present in another array.(我正在寻找一种有效的方法来从javascript数组中删除所有元素(如果它们存在于另一个数组中)。)
// If I have this array:
var myArray = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
// and this one:
var toRemove = ['b', 'c', 'g'];
I want to operate on myArray to leave it in this state: ['a', 'd', 'e', 'f']
(我想对myArray进行操作,以使其保持这种状态: ['a', 'd', 'e', 'f']
)
With jQuery, I'm using grep()
and inArray()
, which works well:(使用jQuery,我使用的是grep()
和inArray()
,效果很好:)
myArray = $.grep(myArray, function(value) {
return $.inArray(value, toRemove) < 0;
});
Is there a pure javascript way to do this without looping and splicing?(有没有这样做而不循环和拼接的纯JavaScript方法?)
ask by Tap translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…