The following figure shows some methods of re-arraying that I summarized when I learned to simulate arrays by myself:
In this article, we will not discuss the method of not changing the original array, only the 6 methods used to change the original array.
How to change the original array
push()
Add elements to the end of the array in the order of the parameters, and return the length of the new array
var color = ['red', 'green']
var color2 = color2.push(['blue','purple'])
alert(color) // ['red', 'green']
alert(color2) // ['red', 'green','blue','purple']
Rewrite:
Array.prototype._push = function() {
for(let i = 0; i < arguments.length; i++) {
this[this.length] = arguments[i]
}
return this.length
}
var arr1 = [1, 2, 3]
console.log(arr1.push(4, 5)) // 返回新数组的长度 5
console.log(arr1._push(6, 7)) // 返回新数组的长度 7
console.log(arr1) // [1, 2, 3, 4, 5, 6, 7]
pop()
Delete the last element in the array and return it
var color = ['red', 'green','blue','purple']
var color2 = color.pop()
alert(color) // ['red','green','blue']
alert(color2) // ['purple']
Array.prototype._pop() = function() {
if(this.length) {
let res = this[this.length - 1]
delete this[this.length]
this.length--
return res
}
}
let arr2 = [1, 2, 3, 4, 5]
console.log(arr2.pop()) // 返回删除的元素 5
console.log(arr2._pop()) // 返回删除的元素 4
console.log(arr2) // [1, 2, 3]
sort()
By default, sort() will rearrange the array elements in ascending order, that is, the smallest value is in the front and the largest value is in the back. Therefore, sort() calls the string() conversion function on each item, and then compares the strings to determine the order. Even if the elements of the array are all numeric values, the array will be converted to a string before comparison and sorting. E.g:
let values = [0, 1, 5, 10, 15]
values.sort()
console.log(values) //0,1,10,15,5
The order of the values in the array is correct at first, but calling sort() will reorder these values in string form. So it can receive a comparison function to determine which value should be ranked first.
function compare(a, b) {
if(a < b) return -1
else if(a > b) return 1
else return 0
}
This comparison function can be applied to most data types, you can pass it as a parameter to sort(), for example:
let values = [0, 1, 5, 10, 15]
values.sort(compare)
console.log(values) // 0,1,5,10,15
Of course, you can also make the sorting produce a descending effect, just exchange the return value:
function compare(a, b) {
if(a < b) return 1
else if(a > b) return -1
else return 0
}
let values = [0, 1, 5, 10, 15]
values.sort(compare)
console.log(values) // 15,10,5,1,0
Rewrite:
var arr = [4, 1, 6, 9, 3, 2, 8, 7]
var arr2 = [4, 1, 6, 9, 3, 2, 8, 7]
console.log(arr.sort());
Array.prototype.mySort = function (arr) {
for (var i = 0; i < this.length; i++) {
for (var j = i + 1; j < this.length; j++) {
if (this[i] > this[j]) {
var temp = this[i]
this[i] = this[j]
this[j] = temp;
}
}
}
return this
}
console.log('mySort:',arr2.mySort());
reverse()
Flash back the array and change the original array
Array.prototype.myReverse = function () {
var left = 0,
right = this.length - 1;
while (left < right) {
var temp = this[left];
this[left] = this[right];
this[right] = temp;
left++;
right--;
}
}
var arr2 = [1, 3, 5, 7, 9]
console.log('before:', arr2)
myReverse(arr2)
console.log('after:', arr2)
shift()
Delete the first element of the array and return it
var arr = [1, 3, 5, 7]
console.log(arr.unshift(9))
console.log(arr)
Array.prototype.myUnshift = function () {
var L = this.length;
var newArr = arguments.length
for (var i = L + newArr - 1; i >= 0; i--) {
if (i > newArr - 1) {
this[i] = this[i - newArr];
} else {
this[i] = arguments[i];
}
}
return this.length;
}
var arr2 = [1, 3, 5, 7,]
console.log('myUnshift:', arr2.myUnshift(9));
console.log('myUnshift:', arr2)
unshift()
Add one or more elements to the beginning of the array and return the new length
var arr = [1, 3, 5, 7]
console.log(arr.unshift(9))
console.log(arr)
Array.prototype.myUnshift = function () {
var L = this.length;
var newArr = arguments.length
for (var i = L + newArr - 1; i >= 0; i--) {
if (i > newArr - 1) {
this[i] = this[i - newArr];
} else {
this[i] = arguments[i];
}
}
return this.length;
}
var arr2 = [1, 3, 5, 7,]
console.log('myUnshift:', arr2.myUnshift(9));
console.log('myUnshift:', arr2)
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。