JSFiddle - React, Tailwind, and code Playground
by arackaf
JavaScript
const people = [
{ name: "Adam", age: 30 },
{ name: "Nick", age: 24 },
{ name: "Becky", age: 45 },
{ name: "Jason", age: 19 },
]
Array.prototype.filter = function(cb) {
const result = [];
for (let i = 0; i < this.length; i++) {
const el = this[i];
if (cb(el, i, this)) {
result.push(el);
}
}
return result;
}
Array.prototype.map = function(cb) {
const result = [];
for (let i = 0; i < this.length; i++) {
const el = this[i];
result.push(cb(el, i, this));
}
return result;
}
Array.prototype.reduce = function(cb, initial) {
const firstIndex = 0;
if (initial === void 0) {
initial = this[0];
firstIndex = 1;
}
for (let i = firstIndex; i < this.length; i++) {
initial = cb(ini)
result.push(cb(el, i, this));
}
return result;
}
const filteredPeople = people
.filter(p => p.age < 40)
.map(p => ({ name: p.name }))
.reduce((str, p) => (str += p.name, str), "");
console.log(filteredPeople);