从另一个数组中删除数组的内容。

时间:2022-09-25 08:10:59

I have two arrays

我有两个数组

var array1 = new Array ["a", "b", "c", "d", "e"];
var array2 = new Array ["a", "c", "d"];

I want to remove elements of array2 from array1

我想从array1中移除array2的元素

Result ["b", "e"]

结果(“b”,“e”)

Is there anything like

有什么像

array1 = array1.remove(array2)

Note I'm using jquery-1.9.1

请注意我用jquery-1.9.1

5 个解决方案

#1


8  

Try:

试一试:

var diff = $(array1).not(array2).get();

#2


2  

function difference(source, toRemove) {
    return source.filter(function(value){
        return toRemove.indexOf(value) == -1;
    });
}

NOTE: Array.prototype.indexOf and Array.prototype.filter are not available before IE9!

注意:Array.prototype。indexOf和Array.prototype。IE9之前没有过滤器!

#3


1  

Although lot of ways to achieve it through native java script but i recommend to see Underscore library

虽然有很多方法可以通过本机java脚本实现,但是我推荐您查看下划线库

#4


1  

Underscore JS is what you need. This library has lots of useful array manipulation functions. Underscore JS

需要使用下划线JS。这个库有很多有用的数组操作函数。强调JS

#5


0  

Underscore.js library helps: Hers is what you need

下划线。js库很有用:你需要她的

_.difference(array1, array2);

#1


8  

Try:

试一试:

var diff = $(array1).not(array2).get();

#2


2  

function difference(source, toRemove) {
    return source.filter(function(value){
        return toRemove.indexOf(value) == -1;
    });
}

NOTE: Array.prototype.indexOf and Array.prototype.filter are not available before IE9!

注意:Array.prototype。indexOf和Array.prototype。IE9之前没有过滤器!

#3


1  

Although lot of ways to achieve it through native java script but i recommend to see Underscore library

虽然有很多方法可以通过本机java脚本实现,但是我推荐您查看下划线库

#4


1  

Underscore JS is what you need. This library has lots of useful array manipulation functions. Underscore JS

需要使用下划线JS。这个库有很多有用的数组操作函数。强调JS

#5


0  

Underscore.js library helps: Hers is what you need

下划线。js库很有用:你需要她的

_.difference(array1, array2);