Thursday, 30 March 2017

Amazing Method Check an Object is Exist in Arraylist

Method to Check an object is exist into the arraylist using JavaScript 

Following the common and very useful method that check an object is exist into the array list with object type.

var CheckObjectExist = function (obj, arrayListObject) {
        var i;
        for (i = 0; i < arrayListObject.length; i++) {
            if (arrayListObject[i] === obj) {
                return true;

        return false;

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 infomation object that will be inserted in "employeeList" array list

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

here i am call "CheckObjectExist" method that is created above of this article. this method will match with object "employeeInfo" is already exist into "employeeList". if not exist that object will push into the "employeeList" array list and if exit , it will give the information that employee already added on the list.

if (CheckObjectExist(employeeInfo , employeeList))
console.log("Employee Already added on the list!");



Employee Already added on the list!


No comments:

Post a Comment

Note: only a member of this blog may post a comment.