0

How can I remove specific items from an array with jQuery?

var rundhalsArray = ["50237451_001", "50237451_100"];

var Array = ["50237451_001", "50237451_100", "50236765_001", "50236765_100"];

I'd like to remove from Array all the items in rundhalsArray.

reto
  • 9,995
  • 5
  • 53
  • 52
user1937021
  • 10,151
  • 22
  • 81
  • 143
  • You can refer this : (http://stackoverflow.com/questions/3596089/how-to-add-and-remove-array-value-in-jquery) – Nish Dec 11 '13 at 14:50

3 Answers3

1
var rundhalsArray = ["50237451_001", "50237451_100"];
var _Array = ["50237451_001", "50237451_100", "50236765_001", "50236765_100"];

$.each(rundhalsArray, function(k,v){
    $.each(_Array, function(k2,v2){
        if(v===v2) _Array.splice(k2,1);
    });
});

console.log(_Array);

http://jsfiddle.net/df5L3/

php_nub_qq
  • 15,199
  • 21
  • 74
  • 144
0
var a = [ "50237451_001", "50237451_100", "50236765_001", "50236765_100" ];
var b = [ "50237451_001", "50237451_100" ];

var minus = function ( a, b ) {
    return a.filter(function ( name ) {
        return b.indexOf( name ) === -1;
    });
};            

var result = minus( a, b );

document.write( result );

Here is a JSFIDDLE with working example.

TheCarver
  • 19,391
  • 25
  • 99
  • 149
0

You could use following code:

DEMO

Array.filter(function(e){return !~$.inArray(e,rundhalsArray)});

$.inArray() is to support older browsers, you could use Array.prototype.indexOf method instead.

A. Wolff
  • 74,033
  • 9
  • 94
  • 155