原始地址:https://github.com/30-seconds...

sample

随机返回给定数组中的元素。

使用 Math.random() 生成一个 0 ~ 1 之间的随机数,然后乘以数组的长度可以得到和数组索引接近的随机数,最后使用 Math.floor() 对随机数向下取整可得到最后结果的索引。

const sample = arr => arr[Math.floor(Math.random() * arr.length)];// examplesample([3, 7, 9, 11]); // 9

sampleSize

返回给定数组中的 n 个值。

使用洗牌算法对数组的元素进行混洗。使用 Array.prototype.slice() 截取数组中的前 n 的元素作为最后的结果。

const sampleSize = ([...arr], n = 1) => {    let m = arr.length;·    while (m) {        const i = Math.floor(Math.random() * m--);        [arr[m], arr[i]] = [arr[i], arr[m]];    }    return arr.slice(0, n);};// examplesampleSize([1, 2, 3], 2); // [3,1]sampleSize([1, 2, 3], 4); // [2,3,1]

shank

具有与 Array.prototype.splice() 相同的功能,但返回一个新数组而不是改变原始数组。

使用 Array.prototype.slice() 获取指定索引之内的数组,使用 Array.prototype.concat() 将截取到的数组与给定的元素拼接到一起,然后使用 Array.prototype.slice() 截取被移除的最后元素索引到结束位置,最后再使用 Array.prototype.concat() 进行拼接。

const shank = (arr, index = 0, delCount = 0, ...elements) =>    arr        .slice(0, index)        .concat(elements)        .concat(arr.slice(index + delCount));// own understandconst shank = (arr, index = 0, delCount = 0, ...elements) => {    return [...arr.slice(0, index), ...elements, ...arr.slice(index + delCount)];};// exampleconst names = ['alpha', 'bravo', 'charlie'];const namesAndDelta = shank(names, 1, 0, 'delta'); // [ 'alpha', 'delta', 'bravo', 'charlie' ]const namesNoBravo = shank(names, 1, 1); // [ 'alpha', 'charlie' ]console.log(names); // ['alpha', 'bravo', 'charlie']

shuffle

随机化给定数组中的元素顺序,返回一个新的数组。使用洗牌算法重新排序数组中的数据。

const shuffle = ([...arr]) => {    let m = arr.length;    while (m) {        const i = Math.floor(Math.random() * m--);        [arr[m], arr[i]] = [arr[i], arr[m]];    }    return arr;};// exampleshuffle([1, 2, 3]); // [2, 3, 1]

similarity

对两个数组进行取交集。

使用 Array.prototype.filter() 对数据进行过滤,使用 Array.prototype.includes() 校验数组的元素是否存在另一个数组。

const similarity = (arr, values) => arr.filter(v => values.includes(v));// examplesimilarity([1, 2, 3], [1, 2, 4]); // [1, 2]

sortedIndex

返回将值插入已排序数组的最低索引,以便不影响其顺序。

首先需要校验数组是升序或降序,使用 Array.prototype.findIndex() 从数组中找出给定值插入的索引。

const sortedIndex = (arr, n) => {    // 判断升序、降序    const isDescending = arr[0] > arr[arr.length - 1];    const index = arr.findIndex(el => (isDescending ? n >= el : n <= el));    return index === -1 ? arr.length : index;};// examplesortedIndex([5, 3, 2, 1], 4); // 1sortedIndex([30, 50], 40); // 1

sortedIndexBy

将值插入已排序数组的最低索引,以便不影响其应用给定迭代器函数后的顺序。

首先需要校验数组是升序或降序,然后对给定的值使用迭代器函数,使用 Array.prototype.findIndex() 从数组中找出给定值使用迭代器函数后得到的值插入的索引。

const sortedIndexBy = (arr, n, fn) => {    const isDescending = fn(arr[0]) > fn(arr[arr.length - 1]);    const val = fn(n);    const index = arr.findIndex(el => (isDescending ? val >= fn(el) : val <= fn(el)));    return index === -1 ? arr.length : index;};// examplesortedIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, o => o.x); // 0

sortedLastIndex

返回将值插入已排序数组的最高索引,以便不影响其顺序。

首先需要校验数组是升序或降序,使用 Array.prototype.reverse() 对数组的元素进行倒序排列,然后使用 Array.prototype.findIndex() 从数组中找出给定值插入的索引。

const sortedLastIndex = (arr, n) => {    const isDescending = arr[0] > arr[arr.length - 1];    const index = arr.reverse().findIndex(el => (isDescending ? n <= el : n >= el));    return index === -1 ? 0 : arr.length - index;};// examplesortedLastIndex([10, 20, 30, 30, 40], 30); // 4

sortedLastIndexBy

将值插入已排序数组的最高索引,以便不影响其应用给定迭代器函数后的顺序。

首先需要校验数组是升序或降序,然后对给定的值使用迭代器函数,使用 Array.prototype.map() 对数组中的元素应用迭代器函数获取新的数组,然后使用 Array.prototype.reverse() 反转数组,最后使用 Array.prototype.findIndex() 找出数组给定给定值使用迭代器函数后得到的值插入的索引。

const sortedLastIndexBy = (arr, n, fn) => {    const isDescending = fn(arr[0]) > fn(arr[arr.length - 1]);    const val = fn(n);    const index = arr        .map(fn)        .reverse()        .findIndex(el => (isDescending ? val <= el : val >= el));    return index === -1 ? 0 : arr.length - index;};// examplesortedLastIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, o => o.x); // 1

stableSort

对数组执行稳定排序,值相同的时候使用索引进行排序,这个方法不改变原始数组,而是返回一个新数组。

使用 Array.prototype.map() 获取出数组的值和索引,使用 Array.prototype.sort() 对数组使用比较器函数进行排序,最后对排序后的数组使用 Array.prototype.map() 获取出数组的值。

const stableSort = (arr, compare) =>    arr        .map((item, index) => ({ item, index }))        .sort((a, b) => compare(a.item, b.item) || a.index - b.index)        .map(({ item }) => item);// exampleconst arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];const stable = stableSort([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], () => 0); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]