array原型方法实现
push pop unshift shift slice splice reverse sort reduce reduceRight concat join every some filter map indexOf lastIndexOf find findIndex includes
let arr = [0, 1, 2, 3]
let len = arr.push(4);
const prototype = Array.prototype;
prototype.push1 = function () {
let len = this.length;
let args = arguments;
for (var i = 0; i < args.length; i++) {
this[len] = args[i];
len++
}
return this.length;
}
let item = arr.pop();
prototype.pop1 = function () {
let lastItem = this[this.length - 1];
this.length = this.length - 1;
return lastItem;
}
let len1 = arr.unshift(-1, -2);
prototype.unshift1 = function () {
let args = arguments;
let len = this.length;
for (var i = len - 1; i >= 0; i--) {
this[i + args.length] = this[i]
}
for (var j = 0; j < args.length; j++) {
this[j] = args[j]
}
return this.length;
}
let firstItem = arr.shift();
prototype.shift1 = function () {
let firstItem = this[0];
let len = this.length;
for (var i = 0; i < len; i++) {
this[i] = this[i + 1]
}
this.length--;
return firstItem;
}
prototype.slice1 = function () {
let arr = []
let start = arguments[0] || 0;
if (start < 0) {
start = this.length + start;
}
let end = arguments[1] || this.length;
if (end > this.length) {
end = this.length;
}
if (end < 0) {
end = this.length + end;
}
if (start >= end) {
return arr;
}
for (var i = 0; i < this.length; i++) {
if (i >= start && i < end) {
arr.push(this[i])
}
}
return arr;
}
prototype.splice1 = function () {
let start = arguments[0] || 0;
let deleteNum = arguments[1] || 0;
let len = this.length;
let add = [];
for (var i = 2; i < arguments.length; i++) {
add.push(arguments[i]);
}
let end = start + deleteNum > len ? len : start + deleteNum;
let deleteItems = this.slice(start, end)
let newArr = this.slice(0, start);
let next = this.slice(start + end);
add.forEach(item => newArr.push(item));
next.forEach(item => newArr.push(item));
for (var j = 0; j < newArr.length; j++) {
this[j] = newArr[j]
}
return deleteItems;
}
console.log(arr, arr.splice1(0, 1, 33, 44, 55, 12))
prototype.reverse1 = function () {
let arr = this.slice(0)
let j = 0;
for (var i = arr.length - 1; i >= 0; i--) {
this[j] = arr[i]
j++
}
return this;
}
let arr1 = [{
n: 2
}, {
n: 1
}, {
n: 11
}, {
n: 22
}]
let arr2 = [{
name: 'AAS'
}, {
name: 'DSF'
}, {
name: 'FSD'
}, {
name: 'ABC'
}]
prototype.sort1 = function (callback) {
for (let i = 0; i < this.length; i++) {
for (let j = 0; j < this.length - 1 - i; j++) {
let result;
if (typeof callback === 'undefined') {
result = (this[j]).toString() > (this[j + 1]).toString();
} else if (typeof callback(this[j], this[j + 1]) === 'number') {
result = callback(this[j], this[j + 1]) > 0;
} else {
result = callback(this[j], this[j + 1]);;
}
if (result) {
let temp = this[j];
this[j] = this[j + 1];
this[j + 1] = temp;
}
}
}
return this;
}
prototype.reduce1 = function (callback, init) {
for (var i = 0; i < this.length; i++) {
if (init === undefined) {
init = callback(this[i], this[i + 1], i + 1, this)
i++;
} else {
init = callback(init, this[i], i, this)
}
}
return init;
}
prototype.reduceRight1 = function(callback, init) {
let len = this.length;
for (var i = len - 1; i >= 0; i--) {
if (init === undefined) {
init = callback(this[i], this[i - 1], i - 1, this)
i--;
} else {
init = callback(init, this[i], i, this)
}
}
return init;
}
let concat1 = arr.concat([4,6,9], 10, 12)
console.log(concat1)
prototype.concat1 = function() {
let args = arguments;
let newArr = []
for (var j = 0; j < this.length; j++) {
newArr.push(this[j])
}
for (var i = 0; i < args.length; i++) {
if (args[i] instanceof Array) {
for (var k = 0; k < args[i].length; k++) {
newArr.push(args[i][k])
}
} else {
newArr.push(arr[i]);
}
}
return newArr;
}
prototype.join1 = function(de) {
de = de || ',';
let str = '';
for (let i = 0;i < this.length; i++) {
str = str + de + this[i];
}
return str.slice(1);
}
prototype.every1 = function(callback) {
let flag = true;
for (let i = 0; i < this.length; i++) {
if (!callback(this[i], i, this)) {
flag = false;
break;
}
}
return flag;
}
prototype.some1 = function(callback) {
let flag = false;
for (let i = 0; i < this.length; i++) {
if (callback(this[i], i, this)) {
flag = true;
break;
}
}
return flag;
}
prototype.filter1 = function(callback) {
let arr = [];
for (let i = 0; i < this.length; i++) {
if (callback(this[i], i, this)) {
arr.push(this[i])
}
}
return arr;
}
let m1 = [{age: 1}, {age: 11}, {age: 22}];
prototype.map1 = function(callback) {
let arr = [];
for (let i = 0; i < this.length; i++) {
arr.push(callback(this[i], i, this))
}
return arr;
}
prototype.indexOf1 = function(item, n) {
let index = -1;
n = n || 0;
for (let i = n; i < this.length; i++) {
if (item === this[i]) {
index = i
break
}
}
return index;
}
prototype.lastIndexOf1 = function(item, n) {
let index = -1;
n = n || this.length - 1;
if (n >= this.length) {
n = this.length - 1;
}
if (n < 0 && Math.abs(n) > this.length) {
return index;
}
for (let i = n; i >= 0; i--) {
if (item === this[i]) {
index = i;
}
}
return index;
}
console.log(arr.lastIndexOf1(2));
prototype.find1 = function(callback) {
let result;
for (let i = 0; i < this.length; i++) {
if (callback(this[i], i, this)) {
result = this[i]
break;
}
}
return result;
}
prototype.findIndex1 = function(callback) {
let index = -1;
for (let i = 0; i < this.length; i++) {
if (callback(this[i], i, this)) {
index = i;
break;
}
}
return index;
}
console.log(m1.includes(11));
prototype.includes1 = function(item, n) {
let flag = false;
n = (n || 0) < 0 ? n + this.length : (n || 0)
for (let i = n; i < this.length; i++) {
if (item === this[i]) {
flag = true;
}
}
return flag;
}
console.log(arr.includes1(2));