forEach
Array.prototype.c_forEach = function(callback){
for(let i=0;i<this.length;i++){
callback(this[i],i,this)
}
}
map
Array.prototype.c_map = function(callback){
let result = [];
for(let i=0;i<this.length;i++){
result.push(callback(this[i],i,this));
}
return result;
}
every
Array.prototype.c_every = function(callback){
for(let i=0;i<this.length;i++){
if(!callback(this[i],i,this)){
return false
}
}
return true
}
some
Array.prototype.c_some = function(callback){
for(let i=0;i<this.length;i++){
if(callback(this[i],i,this)){
return true
}
}
return false
}
reduce
Array.prototype.c_reduce = function (callback, ...args) {
let start = 0,
pre;
if (args.length) {
pre = args[0];
} else {
pre = this[0];
start = 1;
}
for (let i = start; i < this.length; i++) {
pre = callback(pre, this[i], i, this);
}
return pre;
};
find
Array.prototype.c_find = function (callback) {
for (let i = 0; i < this.length; i++) {
if (callback(this[i], i, this)) {
return this[i];
}
}
return undefined;
};
findIndex
Array.prototype.c_findIndex = function (callback) {
for (let i = 0; i < this.length; i++) {
if (callback(this[i], i, this)) {
return i
}
}
return -1;
};
fill
Array.prototype.c_fill = function (value, start = 0, end) {
end = end || this.length;
for (let i = start; i < end; i++) {
this[i] = value;
}
return this;
};
includes
Array.prototype.c_includes = function (value, start = 0) {
if (start < 0) start = start + this.length;
let isNaN = Number.isNaN(value);
for (let i = start; i < this.length; i++) {
if (this[i] === value || (isNaN && Number.isNaN(this[i]))) {
return true;
}
}
return false;
};
join
Array.prototype.c_join = function (value = ",") {
let result = "";
for (let i = 0; i < this.length; i++) {
result = i === 0 ? `${result}${this[i]}` : `${result}${value}${this[i]}`;
}
return result;
};