Javascript Array find(def)

Description

Javascript Array find(def)


Array.prototype.find =  function (def) {
    var array = this;
    var newArray = [];
    array.forEach(function(y) {

        if(def(y)) {
           newArray.push(y);/* w w  w .  jav  a 2s. c o  m*/
        }
    });

    return newArray;
};

// Test Case 1
var y = [1,2,3,4,9,11].find(function(x){return x>2});
console.log(y);


// Syntax to Execute:

[/* Put your array here */].find(/* Put your function here */)



PreviousNext

Related