方法對數組中的每一個元素執行一個由您提供的reducer函數(升序執行),將其結果彙總爲單個返回值。數組
let r = [1, 2, 3, 4].reduce((a, b, index, arr) => a + b);
console.log(r); // 10
/* * 參數:兩個(cb,prev) * 是否改變原數組:否 */
複製代碼
Array.prototype.reduce = function (cb,prev) {
for (let i = 0; i < this.length; i++) {
if (typeof prev === 'undefined') {
prev = cb(this[i], this[i + 1], i + 1, this);
i++;
} else {
prev = cb(prev, this[i], i, this);
}
}
return prev;
}
複製代碼
返回一個映射後的數組函數
let r = [1, 2, 3, 4].map((item, index, array) => {
return item * 2;
});
console.log(r); // [ 2, 4, 6, 8]
/* * 參數:兩個(cb,sourceArray) * 是否改變原數組:否 */
複製代碼
Array.prototype.map = function (cb) {
const newArr = [];
if (typeof cb !== 'function') throw new TypeError(cb + 'is not a function');
for (let i = 0; i < this.length; i++) {
newArr[i] = cb(this[i],i,this)
}
return newArr;
}
複製代碼
返回一個過濾後的數組ui
let r = [1, 2, 3, 2, 4].filter((item, index, array) => {
return item === 2;
})
console.log(r);//[ 2, 2 ]
/* * 參數:兩個(cb,sourceArray) * 是否改變原數組:否 */
複製代碼
Array.prototype.filter = function (cb) {
const newArray = [];
if (typeof cb !== 'function') throw new TypeError(cb + 'is not a function');
for (let i = 0; i < this.length; i++) {
if (cb(this[i], i, this)) {
newArray[newArray.length] = this[i];
}
}
return newArray;
}
複製代碼
找到true就返回truethis
let r = [1, 2, 3, 4].some((item, index, array) => {
return item > 2;
})
console.log(r);// true
/* * 參數:兩個(cb,sourceArray) * 是否改變原數組:否 */
複製代碼
Array.prototype.some = function (cb) {
if (typeof cb !== 'function') throw new TypeError(cb + 'is not a function');
for (let i = 0; i < this.length; i++) {
if (cb(this[i], i, this)) {
return true;
}
}
return false
}
複製代碼
找到false就返回falsespa
let r = [1, 2, 3, 4].every((item, index, array) => {
return item > 1;
});
console.log(r);// false
/* * 參數:兩個(cb,sourceArray) * 是否改變原數組:否 */
複製代碼
Array.prototype.every = function (cb) {
if (typeof cb !== 'function') throw new TypeError(cb + 'is not a function');
for (let i = 0; i < this.length; i++) {
if (!cb(this[i], i, this)) return false;
}
return true;
}
複製代碼
找到後返回找到的那一項prototype
let r = [1, 2, 3, 4, 5, 2].find((item, index,array) => {
return item > 3;
})
console.log(r);// 4
/* * 參數:兩個(cb,sourceArray) * 是否改變原數組:否 */
複製代碼
Array.prototype.find = function (cb) {
if (typeof cb !== 'function') throw new TypeError(cb + 'is not a function');
for (let i = 0; i < this.length; i++) {
if (cb(this[i], i, this)) {
return this[i];
}
}
}
複製代碼
找到後返回找到的那一項的索引code
let r = [1, 2, 3, 4].findIndex((item, index, array) => {
return item > 2;
});
console.log(r);// 2
/* * 參數:兩個(cb,sourceArray) * 是否改變原數組:否 */
複製代碼
Array.prototype.findIndex = function (cb) {
if (typeof cb !== 'function') throw new TypeError(cb + 'is not a functions');
for (let i = 0; i < this.length; i++) {
if (cb(this[i], i, this)) {
return i;
}
}
return -1
}
複製代碼
判斷是否包含某一項索引
let r0 = [1, 2, 3].includes(2);
console.log(r0); // true
let r1 = [1, 2, 3].includes(2,2);
console.log(r1); // false
/* * 參數:兩個(valueToFind,fromIndex) * 是否改變原數組:否 */
複製代碼
Array.prototype.includes = function (valueToFind, fromIndex) {
if (fromIndex > this.length) return false;
for (let i = fromIndex || 0; i < this.length; i++) {
if (this[i] === valueToFind) return true;
}
return false;
}
複製代碼