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
  • 8,367
  • 4
  • 47
  • 50
user1937021
  • 8,155
  • 20
  • 68
  • 129
  • 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
  • 12,762
  • 17
  • 59
  • 123
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
  • 18,072
  • 24
  • 91
  • 146
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
  • 72,298
  • 9
  • 84
  • 139