Array.push()方法
function array_push(arr) {
if (arr == undefined || arr.constructor !== Array)
throw new Error('错误参数');
if (arguments.length === 1) return arr.length;
for (var i = 1; i < arguments.length; i++) {
arr[arr.length] = arguments[i];
}
// 给数组尾部一旦添加元素,arr.length就会自动重设
return arr.length;
}
var arr = [];
var len = array_push(arr, 3, 4, 6, 2, 1);
console.log(len, arr);
Array.pop()方法
function array_pop(arr) {
var item = arr[arr.length - 1];
arr.length--;
return item;
}
var arr = [4, 3, 2, 1];
var item = array_pop(arr);
console.log(arr, item);
Array.unshift()方法
function array_unshift(arr) {
if (arr == undefined || arr.constructor !== Array)
throw new Error('错误参数');
if (arguments.length === 1) return arr.length;
var len = arguments.length - 1; //当前添加参数数量
arr.length = len = len + arr.length; //添加元素后数组的新长度
while (len > 0) {
if (len > arguments.length - 1) {
arr[len - 1] = arr[len - arguments.length];
} else {
arr[len - 1] = arguments[len];
}
len--;
}
return arr.length;
}
var arr = [1, 2, 3, 4];
array_unshift(arr, 0, 1, 23, 5);
console.log(arr);
Array.shift()方法
function array_shift(arr) {
if (arr.length === 0) return;
var item = arr[0];
for (var i = 0; i < arr.length - 1; i++) {
arr[i] = arr[i + 1];
}
arr.length--;
return item;
}
var arr = [];
var item = array_shift(arr);
console.log(item, arr);
Array.concat()方法
function array_concat(arr) {
if (arr == undefined || arr.constructor !== Array)
throw new Error('不是一个数组');
var arr1 = [];
for (var i = 0; i < arr.length; i++) {
arr1[i] = arr[i];
}
if (arguments.length === 1) return arr1;
for (var j = 1; j < arguments.length; j++) {
if (arguments[j] && arguments[j].constructor === Array) {
for (var k = 0; k < arguments[j].length; k++) {
arr1[arr1.length] = arguments[j][k];
}
} else arr1[arr1.length] = arguments[j];
}
return arr1;
}
var arr = [1, 2, 3, 4];
var arr1 = array_concat(arr);
arr[0] = 100;
Array.join()方法
function array_join(arr, separator) {
if (arr == undefined || arr.constructor !== Array)
throw new Error('不是一个数组');
if (separator === undefined) separator = ',';
separator = String(separator);
var str = '';
for (var i = 0; i < arr.length - 1; i++) {
str += arr[i] + separator;
}
str += arr[arr.length - 1];
return str;
}
var arr = [1, 2, 3, 4];
var str = array_join(arr, '');
console.log(str);
Array.fill()方法
function array_fill(arr, item, start, end) {
if (arr == undefined || arr.constructor !== Array)
throw new Error('不是一个数组');
if (start === undefined) start = 0;
if (end === undefined) end = arr.length;
for (var i = start; i < end; i++) {
arr[i] = item;
}
return arr;
}
var arr = Array(10);
arr = array_fill(arr, 5, 2, 5);
console.log(arr);
Array.isArray()方法
Array.from_1 = function (list) {
var arr = [];
if (!list.length) return arr;
for (var i = 0; i < list.length; i++) {
arr[i] = list[i];
}
return arr;
return Array.prototype.slice.apply(list);
//return Array.prototype.slice.call(list);
//return [].slice.apply(list);
//return [].slice.call(list);
//return [].concat.apply([], list);
//return Array.prototype.concat.apply([], list);
};
Array.splice()方法
function array_splice(arr, start, deleteCount) {
var arr1 = [];
if (arr == undefined || arr.constructor !== Array)
throw new Error('参数错误');
if (start === undefined) return arr1;
start = Number(start);
if (isNaN(start)) start = 0;
if (start < 0) start = start + arr.length < 0 ? 0 : start + arr.length;
if (start > arr.length) start = arr.length;
if (deleteCount === undefined) deleteCount = arr.length - start;
deleteCount = Number(deleteCount);
if (isNaN(deleteCount)) deleteCount = 0;
if (deleteCount < 0) return arr1;
if (deleteCount + start > arr.length) deleteCount = arr.length - start;
for (var i = start; i < arr.length; i++) {
if (i < deleteCount + start) arr1[arr1.length] = arr[i];
arr[i] = arr[i + deleteCount];
}
arr.length -= deleteCount;
var len = arguments.length - 3;
var l = (arr.length += len);
while (l > start) {
if (l > start + len) arr[l - 1] = arr[l - len - 1];
else arr[l - 1] = arguments[3 + (l - 1 - start)];
l--;
}
return arr1;
}
var arr1 = array_splice(arr, 7, 0, -1, -2, -3);
console.log(arr, arr1);
Array.slice()方法
function array_slice(arr, start, end) {
var arr1 = [];
if (isNaN(start)) start = 0;
if (isNaN(end)) end = arr.length;
start = parseInt(start);
end = parseInt(end);
if (start < 0) start = start + arr.length < 0 ? 0 : start + arr.length;
if (end < 0) end = end + arr.length < 0 ? 0 : end + arr.length;
for (var i = start; i < end; i++) {
arr1[arr1.length] = arr[i];
}
return arr1;
}
var arr = [1, 2, 3, 4, 5];
var arr1 = array_slice(arr, 1, -7);
var arr2 = arr.slice(1, -7);
console.log(arr1, arr2);
Array.forEach()方法
function array_forEach(arr, fn) {
for (var i = 0; i < arr.length; i++) {
if (i in arr) fn(arr[i], i, arr);
}
}
var arr = [2, 4, 6, 8, 10];
array_forEach(arr, function (item, index, arr) {
console.log(item, index, arr);
});
Array.map()方法
function array_map(arr, fn) {
var arr1 = [];
for (var i = 0; i < arr.length; i++) {
if (i in arr) arr1[i] = fn(arr[i], i, arr);
}
return arr1;
}
function fn1(item, index, arr) {
return item + 10;
}
var arr = [2, 4, 6, 8, 10];
var arr1 = array_map(arr, fn1);
console.log(arr1);
Array.some()方法
function array_some(arr, fn) {
for (var i = 0; i < arr.length; i++) {
if (i in arr && Boolean(fn(arr[i], i, arr))) return true;
}
return false;
}
Array.every()方法
function array_every(arr, fn) {
for (var i = 0; i < arr.length; i++) {
if (i in arr && !fn(arr[i], i, arr)) return false;
}
return true;
}
var bool = array_every(arr, function (item, index, arr) {
return item > 5;
});
console.log(bool);
Array.filter()方法
function array_filter(arr, fn) {
var arr1 = [];
for (var i = 0; i < arr.length; i++) {
// 回调函数返回为ture时,将元素添加到新数组的尾部
if (i in arr && fn(arr[i], i, arr)) arr1[arr1.length] = arr[i];
}
return arr1;
}
var arr1 = array_filter(arr, function (item) {
return item > 3;
});
console.log(arr1);
Array.reduce()方法
function array_reduce(arr, fn, initValue) {
if (arr.length === 0) return;
if (arr.length === 1) return arr[0];
var index = 1;//后续判断从第几个开始循环,如果有初始值则从第0个开始循环,如果没有初始值从第一个开始循环
var value = arr[0];
if (initValue !== undefined) {
index = 0;
value = initValue;
// 如果初始值存在value就是初始值,如果不存在,初始值就是数组的第0项
}
for (; index < arr.length; index++) {
value = fn(value, arr[index], index, arr);
}
return value;
}
var val = array_reduce(arr, function (value, item) {
console.log(value, item);
return value + item;
})
console.log(val);
Array.flatmap()方法
var arr1 = array_flatmap(arr, function (item) {
if (Array.isArray(item))
item = array_flatmap(item, function (t) {
return t;
});
return item;
});
console.log(arr1);
function array_flatmap(arr, fn) {
var arr1 = [];
for (var i = 0; i < arr.length; i++) {
var value = fn(arr[i], i, arr);
if (value && value.constructor === Array) {
for (var j = 0; j < value.length; j++) {
arr1[arr1.length] = value[j];
}
} else {
arr1[arr1.length] = value;
}
}
return arr1;
}
Array.reverse()方法
var arr = [1, 2, 3, 4, 5, 6];
var arr1 = arr.reverse();
console.log(arr);
function reverse_1(arr) {
for (i = 0; i < parseInt(arr.length / 2); i++) {
var temp = arr[arr.length - i - 1];
arr[arr.length - i - 1] = arr[i];
arr[i] = temp;
}
return arr;
}
reverse_1(arr);
console.log(arr);
Array.sort()方法
var arr = [1, 2, 4, 0, 3, 10, 9, 24, 17, 13, 6, 5];
function sort_1(arr, fn) {
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr.length - 1 - i; j++) {
if (fn(arr[j], arr[j + 1]) > 0) {
var tem = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tem;
}
}
}
}
sort_1(arr, function (a, b) {
return b - a;
});
console.log(arr);
目录