Advertisement

Latest Post

Thursday, 30 March 2017

Important Method To Remove an Object From Object Arraylist


Method to Remove an object from object arraylist using JavaScript
Following is the method that remove an object from the arraylist with object type.

var removeObjectByAttr = function (arrayListObject, key, value) {
        var len = arrayListObject.length;
        while (len--) {
            if (arrayListObject[len]
                && arrayListObject[len].hasOwnProperty(key)
                && (arguments.length > 2 && arr[len][key] === value)) {
                arr.splice(len, 1);
            }
        }
        return arr;
    }

Example:-
I have created  array list "employeeList" that is object type and inserted employees information as the form of object.

var employeeList=[
  {"EMP_ID": "14","EMP_NAME": "alok"},
  {"EMP_ID": "15","EMP_NAME": "anil"},
  {"EMP_ID": "21","EMP_NAME": "sam"},
  {"EMP_ID": "24","EMP_NAME": "deepak"},
  {"EMP_ID": "32","EMP_NAME": "sachin"},
  {"EMP_ID": "37","EMP_NAME": "anand"}
];

Here i am creating a employee information object that will remove from "employeeList" array list

var employeeInfo = new Object();
        employeeInfo.EMP_ID = 14;
        employeeInfo.EMP_NAME = "alok";

here i am call "removeObjectByAttr" method that is created above of this article. by use of this we can remove the "employeeInfo" object from arraylist.

employeeList = removeObjectByAttr(employeeList, 'EMP_ID', serviceInfo.EMP_ID);

OUTPUT:-

[
  {"EMP_ID": "15","EMP_NAME": "anil"},
  {"EMP_ID": "21","EMP_NAME": "sam"},
  {"EMP_ID": "24","EMP_NAME": "deepak"},
  {"EMP_ID": "32","EMP_NAME": "sachin"},
  {"EMP_ID": "37","EMP_NAME": "anand"}
]

Government Jobs