Javascript Array myFilter(callback)

Description

Javascript Array myFilter(callback)


// the global Array
var s = [23, 65, 98, 5];

Array.prototype.myFilter = function(callback) {
  var newArray = [];
  // Add your code below this line
  s.forEach(a => {//from  www . j a v  a 2  s .c o  m
    if (callback(a)) {
      newArray.push(a);
    }
  });
  // Add your code above this line
  return newArray;
};

var new_s = s.myFilter(function(item) {
  return item % 2 === 1;
});

console.log(new_s);

Javascript Array myFilter(callback)

Array.prototype.myFilter = function (callback) {
    var array = this;
    var newArray = [];
    for (var i = 0; i < array.length; i++) {
        if(callback(array[i])) {
            console.log(array[i]);//w  w  w.j  a  v a  2s. c  o m
            newArray.push(array[i]);
        }
    }
   
    return newArray;
};


var numericArray = [8, 3, 4, 32, 1, 9, 3, 5, 42, 56];

var myNewNumberArray = numericArray.myFilter(function(item) {
    return item < 5;
});

var letterArray = ["a", "b", "a"];

var myNewLetterArray = letterArray.myFilter(function(item) {
    return item === "a";
});

Javascript Array myFilter(callback)

//Here we'll apply a custom filter to the Array prototype.
//NB!! Its common standard not to change the native objects that comes with javascript

var myArray = ["Lars", "Knud", "Hans", "Ole"];

Array.prototype.myFilter = function(callback){
    var t = Object(this);
    var localArr = [];

    for(var i in t) {
        var res = callback.call(t, t[i]);
        if(typeof res === "string") {
            localArr.push(res);//from w  ww.j  a  v  a 2  s  .  c  om
        };
    };
    return localArr;
};


console.log(myArray.myFilter(function(name){
    if(name.length <= 3) {
        return name;
    }
}).length);

Javascript Array myFilter(callback)

var names = ["Lars", "Jan", "Peter", "Bo", "Frederik"];

Array.prototype.myFilter = function(callback) {
    var newArray = [];
    for (var i = 0, max = this.length; i < max; i++) {
        if (callback(this[i])) {
            newArray.push(this[i]);//from w  w w.j a  v  a  2 s  .c om
        }
    }
    return newArray;
};

Array.prototype.myMap = function(callback) {
    var newArray = [];
    for (var i = 0, max = this.length; i < max; i++) {
        newArray.push(callback(this[i]));
    }
    return newArray;
};

function getShort(value) {
    return value.length <= 3;
}

function getUppercase(value) {
    return value.toUpperCase();
}

console.log(names.myFilter(getShort));
console.log(names.myMap(getUppercase));

Javascript Array myFilter(callback)

var names = ["Lars", "Jan", "Peter", "Bo", "Frederik"]

Array.prototype.myFilter = function(callback) {
    var newArray = []
    for (var i = 0, max = this.length; i < max; i++) {
        if(callback(this[i]))
            newArray.push(this[i])//from   w  ww .  j a  v  a2s . com
    }
    return newArray
}
function shortNames(value) {
    return value.length <= 3
}
var newNames = names.myFilter(shortNames)
console.log(newNames)



PreviousNext

Related