JavaScript Method to Find an object details from the Arraylist
Following is the JS method that find an object details from the arraylist with object type.
Example:-
I have created array list "employeeList" that is object type and will get employees infomation with key an value.
here i am call "getObjectArraylist" method that is created above of this article. by use of this we can get the object array list from arrary list.
OUTPUT:-
Following is the JS method that find an object details from the arraylist with object type.
var getObjectArraylist = function (ObjectArraylist, key, val) { var objects = []; for (var i in ObjectArraylist) { if (!ObjectArraylist.hasOwnProperty(i)) continue; if (typeof ObjectArraylist[i] == 'object') { objects = objects.concat(getObjectArraylist(ObjectArraylist[i], key, val)); } else if (i == key && ObjectArraylist[key] == val) { objects.push(ObjectArraylist); } } return objects; };
Example:-
I have created array list "employeeList" that is object type and will get employees infomation with key an value.
var employeeList = [ { "EMP_ID": "14", "EMP_NAME": "alok", "EMP_ADDRESS":"delhi" }, { "EMP_ID": "15", "EMP_NAME": "anil", "EMP_ADDRESS": "delhi" }, { "EMP_ID": "21", "EMP_NAME": "sam", "EMP_ADDRESS": "mumbai" }, { "EMP_ID": "24", "EMP_NAME": "deepak", "EMP_ADDRESS": "mohali" }, { "EMP_ID": "32", "EMP_NAME": "sachin", "EMP_ADDRESS": "mohali" }, { "EMP_ID": "37", "EMP_NAME": "anand", "EMP_ADDRESS": "delhi" } ];
here i am call "getObjectArraylist" method that is created above of this article. by use of this we can get the object array list from arrary list.
var result = getObjectArraylist(employeeList, "EMP_ADDRESS", "delhi") console.log(JSON.stringify(result));
OUTPUT:-
[ { "EMP_ID": "14", "EMP_NAME": "alok", "EMP_ADDRESS":"delhi" }, { "EMP_ID": "15", "EMP_NAME": "anil", "EMP_ADDRESS": "delhi" }, { "EMP_ID": "37", "EMP_NAME": "anand", "EMP_ADDRESS": "delhi" } ]
No comments:
Post a Comment
Note: only a member of this blog may post a comment.