Skip to content

Instantly share code, notes, and snippets.

@robertoandres24
Last active May 1, 2020 02:02
Show Gist options
  • Save robertoandres24/96bf6cdfa320f3b880decd9cc3aa6bf5 to your computer and use it in GitHub Desktop.
Save robertoandres24/96bf6cdfa320f3b880decd9cc3aa6bf5 to your computer and use it in GitHub Desktop.
let animals = [
{ name: 'alf', type: 'dog' },
{ name: 'beto', type: 'cat' },
{ name: 'carl', type: 'dog' },
{ name: 'dan', type: 'fish' }
]
let isDog = a => a.type === 'dog'
const myFilter = function (cb) {
const newArr = []
for (let i = 0; i < this.length; i++) {
let truthy = cb(this[i])
truthy && newArr.push(this[i])
}
return newArr
}
Array.prototype.myFilter = myFilter
console.log(animals.myFilter(isDog))
const strArray = ['JavaScript', 'Python', 'PHP', 'Java', 'C'];
function mapForEach(arr, fn) {
const newArray = [];
for(let i = 0; i < arr.length; i++) {
newArray.push(
fn(arr[i])
);
}
return newArray;
}
const lenArray = mapForEach(strArray, function(item) {
return item.length;
});
// prints [ 10, 6, 3, 4, 1 ]
console.log(lenArray);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment