最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove contents of an array from another array - Stack Overflow

programmeradmin5浏览0评论

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

Result ["b", "e"]

Is there anything like

array1 = array1.remove(array2)

Note I'm using jquery-1.9.1

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

Result ["b", "e"]

Is there anything like

array1 = array1.remove(array2)

Note I'm using jquery-1.9.1

Share Improve this question asked Sep 25, 2013 at 7:28 OkkyOkky 10.5k15 gold badges77 silver badges123 bronze badges 1
  • possible duplicate of JavaScript array difference – Itay Commented Sep 25, 2013 at 7:31
Add a ment  | 

5 Answers 5

Reset to default 8

Try:

var diff = $(array1).not(array2).get();
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!

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

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

Underscore.js library helps: Hers is what you need

_.difference(array1, array2);
发布评论

评论列表(0)

  1. 暂无评论