Skip to content Skip to sidebar Skip to footer

Remove Set Of Items From An Array Jquery

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', '5023

Solution 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/

Solution 2:

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.

Solution 3:

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.

Post a Comment for "Remove Set Of Items From An Array Jquery"