pop()
pop() 方法用于删除数组的最后一个元素并返回删除的元素。
注意:此方法改变数组的长度!
// pop()方法重构
Array.prototype.mypop = function() {
var tem = this[this.length - 1]
this.length--;
return tem;
}
var arr = [1, 2, 10, 5, 3];
var num_1 = arr.mypop();
console.log(num_1);
console.log(arr);
push()
push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度。
注意: 新元素将添加在数组的末尾。
注意: 此方法改变数组的长度。
var arr = [1, 2, 10, 5, 3];
// push()
Array.prototype.mypush = function() {
for (var i = 0; i < arguments.length; i++) {
this[this.length] = arguments[i];
}
return this.length;
}
var num = arr.mypush(50, 60, 70);
console.log(num);
console.log(arr);
shift()
shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值。
注意: 此方法改变数组的长度!
// shift()
Array.prototype.myshift = function() {
var tem = this[0];
for (var i = 0; i < this.length; i++) {
this[i] = this[i + 1];
}
this.length--;
return tem;
}
var arr = [1, 2, 10, 5, 3];
var num_1 = arr.myshift();
console.log(num_1);
console.log(arr);
unshift()
unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度。
注意: 该方法将改变数组的数目。
// unshift()
Array.prototype.myunshift = function() {
var sum = this.length + arguments.length;
var ag = arguments.length;
var j;
//方法一
for (i = sum; i > 0; i--) {
if (i > ag) {
this[i - 1] = this[i - 1 - ag];
} else {
this[i - 1] = arguments[i - 1];
}
}
//方法二
// for (j = sum - 1; j > ag; j--) {
// this[j] = this[j - ag];
// }
// if (j == ag) {
// this[j] = this[0];
// }
// for (var i = 0; i < ag; i++) {
// this[i] = arguments[i];
// }
return sum;
}
var arr = [1, 2, 10, 5, 3];
var num_1 = arr.myunshift(50, 30, 60, 70);
console.log(num_1);
console.log(arr);
reverse()
reverse() 方法用于颠倒数组中元素的顺序
var arr = [1, 2, 10, 5, 3];
// reverse()
Array.prototype.myreverse = function() {
var i, j;
for (i = 0, j = this.length - 1; i != j; i++, j--) {
var temp;
temp = this[i];
this[i] = this[j];
this[j] = temp;
}
return this;
}
var num_1 = arr.myreverse(50, 30, 60, 70);
console.log(num_1);
console.log(arr);
concat()
concat() 方法用于连接两个或多个数组。
该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本
// concat
Array.prototype.myconcat = function() {
var arr = new Array();
for (var i = 0; i < this.length; i++) {
arr[i] = this[i];
}
for (var i = 0; i < arguments.length; i++) {
for (var j = 0; j < arguments[i].length; j++) {
arr[arr.length] = arguments[i][j];
}
}
return arr;
}
var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var kai = ["Robin"];
var children = hege.myconcat(stale, kai);
console.log(children);
console.log(hege);
slice()
slice() 方法可从已有的数组中返回选定的元素。
slice()方法可提取字符串的某个部分,并以新的字符串返回被提取的部分
// slice()
Array.prototype.myslice = function(start, end) {
var arr = new Array();
var j = 0;
if (start > 0 && end < this.length) {
for (var i = start; i < end; i++) {
arr[j] = this[i];
j++;
}
} else if (start > 0 && end == null) {
end = this.length;
for (var i = start; i < end; i++) {
arr[j] = this[i];
j++;
}
} else if (start < 0 && end < 0 && start < end) {
start = this.length + start;
end = this.length + end;
for (var i = start; i < end; i++) {
arr[j] = this[i];
j++;
}
} else if (start < 0) {
start = this.length + start;
for (var i = start; i < this.length; i++) {
arr[j] = this[i];
j++;
}
} else {
return arr;
}
return arr;
}
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.myslice(-2);
console.log(citrus);
splice()
splice() 方法用于添加或删除数组中的元素。
注意:这种方法会改变原始数组。
// splice()
Array.prototype.mysplice = function() {
// 给定添加数字
if (isNaN(arguments[1]) == false) {
var ag = arguments.length - 2;
var sum = this.length + ag;
for (var i = sum; i > arguments[0] + 1; i--) {
this[i - 1] = this[i - ag - 1];
}
for (var i = 2; i < arguments.length; i++) {
this[arguments[0]] = arguments[i];
arguments[0]++;
}
}
// 不给定添加数字
if (isNaN(arguments[1]) == true) {
var ag = arguments.length - 2;
var sum = this.length + ag;
for (var i = sum; i > arguments[0] + 1; i--) {
this[i - 1] = this[i - ag - 1];
}
for (var i = 2; i < arguments.length; i++) {
this[arguments[0]] = arguments[i];
arguments[0]++;
}
}
}
var fruits = ["Banana", "Orange", "Apple", "Mango"];
//从数组索引位置 2 开始,不删除元素,添加两个新的元素"Lemon","Kiwi"
fruits.splice(2, 0, "Lemon", "Kiwi", '111');
console.log(fruits);
forEach()
forEach() 方法用于调用数组的每个元素,并将元素传递给回调函数。
注意: forEach() 对于空数组是不会执行回调函数的。
// forEach()
Array.prototype.myforEach = function(fun) {
for (var i = 0; i < this.length; i++) {
fun(this[i], i, this);
}
}
var num_1 = arr.myforEach(function(item, index, arr) {
console.log(item, index, arr);
});
indexOf()
indexOf() 方法可返回数组中某个指定的元素位置。
该方法将从头到尾地检索数组,看它是否含有对应的元素。开始检索的位置在数组 start 处或数组的开头(没有指定 start 参数时)。如果找到一个 item,则返回 item 的第一次出现的位置。开始位置的索引为 0。
如果在数组中没找到指定元素则返回 -1
// indexOf()
Array.prototype.myindexOf = function() {
for (var i = 0; i < this.length; i++) {
if (this[i] == arguments[0]) {
return i;
}
}
}
var fruits = ["Banana", "Orange", "Apple", "Apple"];
var a = fruits.myindexOf("Apple");
console.log(a);
lastIndexOf()
lastIndexOf() 方法可返回一个指定的元素在数组中最后出现的位置,从该字符串的后面向前查找。
如果要检索的元素没有出现,则该方法返回 -1。
该方法将从尾到头地检索数组中指定元素 item。开始检索的位置在数组的 start 处或数组的结尾(没有指定 start 参数时)。如果找到一个 item,则返回 item 从尾向前检索第一个次出现在数组的位置。数组的索引开始位置是从 0 开始的。
如果在数组中没找到指定元素则返回 -1。
// lastIndexOf()
Array.prototype.mylastIndexOf = function() {
for (var i = this.length - 1; i > 0; i--) {
if (this[i] == arguments[0]) {
return i;
}
}
}
var fruits = ["Banana", "Orange", "Apple", "Apple"];
var a = fruits.mylastIndexOf("Apple");
console.log(a);
every()
every() 方法用于检测数组所有元素是否都符合指定条件(通过函数提供)。
every() 方法使用指定函数检测数组中的所有元素:
-
如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测。
-
如果所有元素都满足条件,则返回 true。
注意: every() 不会对空数组进行检测。
注意: every() 不会改变原始数组。
// every
var ages = [32, 33, 17, 40, 14, 50];
Array.prototype.myevery = function(fun,obj) {
if (this.length == 0) {
return false;
} else {
for (var i = 0; i < this.length; i++) {
if (!(obj?fun.bind(obj)(this[i]):fun(this[i], i, this))) {
return false;
}
}
}
return true;
}
var result = ages.myevery(function(age) {
console.log(this);
return age >= 18
},{name:'zhangsan'})
console.log(result);
some()
some() 方法用于检测数组中的元素是否满足指定条件(函数提供)。
some() 方法会依次执行数组的每个元素:
-
如果有一个元素满足条件,则表达式返回true , 剩余的元素不会再执行检测。
-
如果没有满足条件的元素,则返回false。
注意: some() 不会对空数组进行检测。
注意: some() 不会改变原始数组。
var ages = [32, 33, 17, 40, 14, 50];
// some()
Array.prototype.mysome = function(fun,obj) {
if (this.length == 0) {
return false;
} else {
for (var i = 0; i < this.length; i++) {
if (obj?fun.bind(obj)(this[i]):fun(this[i], i, this)) {
return true;
}
}
}
return false;
}
var result = ages.mysome(function(age) {
console.log(this);
return age >= 18
},{name:'zhangsan'})
console.log(result);
filter()
filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。
注意: filter() 不会对空数组进行检测。
注意: filter() 不会改变原始数组。
var ages = [32, 33, 17, 40, 14, 50];
// filter()
Array.prototype.myfilter = function(fun,obj) {
var arr = new Array();
var j = 0;
if (this.length == 0) {
return false;
} else {
for (var i = 0; i < this.length; i++) {
if (obj?fun.call(obj,this[i]):fun(this[i], i, this)) {
arr[j] = this[i];
j++;
}
}
}
return arr;
}
var result = ages.myfilter(function(age) {
console.log(this);
return age >= 18
},{name:'zhangsan'})
console.log(result);
console.log(ages);
map()
map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。
map() 方法按照原始数组元素顺序依次处理元素。
注意: map() 不会对空数组进行检测。
注意: map() 不会改变原始数组
// map()
Array.prototype.mymap = function(fun,obj) {
var arr = new Array();
for (var i = 0; i < this.length; i++) {
arr[i] = obj?fun.bind(obj)(this[i]):fun(this[i], i, this);
}
return arr;
}
var numbers = [4, 9, 16, 25];
var result = numbers.mymap(function(number) {
console.log(this);
return Math.sqrt(number)
},{name:'zhangsan'})
console.log(result);