Sunday, 8 November 2015

How to check Array contains an Item in JavaScript?

Hi,
 
Here is the JavaScript  code to check Array contains an item in JavaScript.
 
var vArrValues = ["1", "2", "3", "4"];

// Check Array Contains Values
Array.prototype.contains = function (element) {
     return this.indexOf(element) > -1;
};

function CheckArrayContaintsValue(vValue) {
    if (vArrValues.contains(vValue)) {
            return true;
    }
    else {
       return false;
     }
}

CheckArrayContaintsValue("1"); // Returns True
CheckArrayContaintsValue("5"); // Returns False
 
Hope this helps.
 
--
Happy Coding.
Gopinath

No comments:

Post a Comment