Wednesday, May 21, 2014

Another way to remove the specific item From n-dimensional array using Jquery


//Array 
var elementsArray = [
        { name: 'First Element', id: 1},
        { name: 'Second Element', id: 2 },
        { name: 'Third Element', id: 3 },
       ];


//Jquery Function to remove the current object from the array
    function RemoveElementFromArray(array, id) {
        if (array != null && array.length > 0) {
            array = $.grep(array, function (value) {
                return value.id != id;
            });
            return array;
        }
        else {
            return null;
        }
    }


//Calling
function remove()
{
//For instance, we want to remove the 2nd element having ID 2

elementsArray  =  RemoveElementFromArray(elementsArray, 2);
for(int i=0; i<elementsArray.length; i++)
{
alert(elementsArray[i].name);
}
}

No comments:

Post a Comment