let arr = [23, 2, 34, 5, 2, 123];
let newArr = arr.sort(function (a, b) {
return b - a
})
console.log(newArr)
let min = arr[0];
for (let i = 0; i < arr.length; i++) {
min = min > arr[i] ? min : arr[i]
}
console.log(min)
for (const [item, index] of arr.entries()) {
console.log(item, index)
}
let min3 = Math.min(...arr);
let max3 = Math.max(...arr)
console.log('min3', min3);
console.log('max3', max3);
console.log('------------')
let ary = [1, 3, 5, 7];
var r = ary.push(9, 10);
console.log(ary);
console.log(r);
Array.prototype.mypush = function (...args) {
for (let i = 0; i < args.length; i++) {
this[this.length] = args[i]
}
return this.length
}
console.log('ary----', ary);
ary.mypush(1, 2, 3, 4)
console.log('ary====', ary)
Array.prototype.mypop = function () {
let last = this.length - 1
this.length = last
return this
}
ary.mypop();
ary.mypop();
ary.mypop();
console.log('pop之后', ary)
Array.prototype.myforeach = function (calback) {
for (let i = 0; i < this.length; i++) {
calback(this[i], i)
}
}
ary.forEach((item, index) => console.log(item, index))
Array.prototype.mymap = function (calback) {
let newArr = []
for (let i = 0; i < this.length; i++) {
calback(this[i], i)
newArr.push(this[i])
}
return newArr
}
let map1 = ary.map((item, index) => {
return item * 10
})
console.log('map====', map1)
|