只是一些比较简单的实现,这里按照字母排序了。
Github 链接:js impl
CSDN下载:csdn
concat
因为 concat 可以参数中可以接受多个数组,所以用了 rest operator 去接受多个数组。
const concat = (arr, arr2) => {
const newArr = [...arr];
for (let i = 0; i < arrs.length; i++) {
const arr2 = arrs[i];
for (let j = 0; j < arr2.length; j++) {
push(newArr, arr2[j]);
}
}
return newArr;
};
find
for (let i = 0; i < arr.length; i++) {
const element = arr[i];
if (cb(element)) return element;
}
return undefined;
flat
flat 是通过递归的方式完成,不过 flat 应该是需要修改原本的数组,那个在递归的操作比较麻烦,这里就直接使用返回一个新的数组的方式了。
const flat = (arr, depth = 1) => {
const newArr = [];
for (let i = 0; i < arr.length; i++) {
const element = arr[i];
if (element instanceof Array && depth > 0) {
push(newArr, ...flat(element, depth - 1));
} else {
push(newArr, element);
}
}
return newArr;
};
forEach & map
在 JavaScript 的 foreach 用不了 break/continue?同样写法下 for 循环也不行 里面大致实现过了,这里就不继续了。
includes
indexOf , lastIndexOf 和 includes 的实现差不多,这里就不重新实现了。
const includes = (arr, val) => {
for (let i = 0; i < arr.length; i++) {
const element = arr[i];
if (element == val) return true;
}
return false;
};
join
const join = (arr, separator = ",") => {
let str = "";
for (let i = 0; i < arr.length - 1; i++) {
str += arr[i] + separator;
}
str += arr[arr.length - 1];
return str;
};
pop
const pop = (arr) => {
const popped = arr[arr.length - 1];
arr.length--;
return popped;
};
push
const push = (arr, ...values) => {
for (let i = 0; i < values.length; i++) {
arr[arr.length] = values[i];
}
};
reduce
const reduce = (arr, reducer, initialValue) => {
let accum = initialValue,
counter = 0;
if (!initialValue) {
accum = arr[0];
counter++;
}
for (let i = counter; i < arr.length; i++) {
accum = reducer(accum, arr[i], i, arr);
}
return accum;
};
reverse
const reverse = (arr) => {
for (let i = 0; i < arr.length / 2; i++) {
[arr[i], arr[arr.length - i - 1]] = [arr[arr.length - 1 - i], arr[i]];
}
};
shift
const shift = (arr) => {
const shifted = arr[0];
for (let i = 1; i < arr.length; i++) {
arr[i - 1] = arr[i];
}
arr.length--;
return shifted;
};
slice
slice 可以接受负值作为参数,这里对此也做了简单的处理。
const slice = (arr, start = 0, end = arr.length) => {
const arrCopy = [];
if (start < 0) start = arr.length + start;
if (end < 0) end = arr.length + end;
for (let i = start; i < end; i++) {
push(arrCopy, arr[i]);
}
return arrCopy;
};
some
every 和 some 实现的方法也差不多,这里就不重新 cv 了。
const some = (arr, cb) => {
for (let i = 0; i < arr.length; i++) {
if (cb(arr[i], i, arr)) return true;
}
return false;
};
sort
sort 原生排序实现根据 runtime 和数组类型不同而有差异,这里就是用最基础的冒泡排序了。
function swap(arr, i, j) {
[arr[i], arr[j]] = [arr[j], arr[i]];
}
function bubbleSort(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = 1; j < arr.length - i; j++) {
if (arr[j - 1] > arr[j]) {
swap(arr, j, j - 1);
}
}
}
}
const sort = (arr, comparator = bubbleSort) => {
comparator(arr);
};
splice
splice 也是一个相对而言比较复杂的方法了,大概实现内容如下:
const subArr = (arr, start, end = arr.length) => {
const subArray = [];
for (let i = start; i < end; i++) {
subArray.push(arr[i]);
}
return subArray;
};
const splice = (arr, start = 0, deleteCount = 0, ...items) => {
let subArray = null;
const deleted = [];
if (items.length > 0 || deleteCount > 0) {
subArray = subArr(arr, start);
arr.length -= arr.length - start;
}
while (deleteCount > 0) {
push(deleted, shift(subArray));
deleteCount--;
}
if (items.length > 0) {
for (let i = 0; i < items.length; i++) {
push(arr, items[i]);
}
}
if (subArray && subArray.length > 0) {
for (let i = 0; i < subArray.length; i++) {
push(arr, subArray[i]);
}
}
return deleted;
};
实现思路就是截取一个 sub array,如果需要删除的话,直接 shift(移除 sub array 头部的内容),随后将 items 和 sub array 中的内容加回原数组即可——splice 会修改原数组。
unshift
const unshift = (arr, val) => {
for (let i = arr.length; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = val;
};
|