2022年9月7日

从数组中获取随机元素

摘要: JavaScript const sample = arr => arr[Math.floor(Math.random() * arr.length)] Examples sample([3, 7, 9, 11]) // 9 阅读全文

posted @ 2022-09-07 11:02 小馬過河﹎ 阅读(15) 评论(0) 推荐(0)

计算两个或多个数字的平均值

摘要: JavaScript const avg = (...nums) => nums.reduce((acc, val) => acc + val, 0) / nums.length Examples avg(...[1, 2, 3]) // 2 avg(1, 2, 3) // 2 阅读全文

posted @ 2022-09-07 10:55 小馬過河﹎ 阅读(39) 评论(0) 推荐(0)

从数组中删除虚假值

摘要: 过滤掉虚假值:false, null, 0, "", undefined, NaN JavaScript const compact = arr => arr.filter(Boolean) Examples compact([0, 1, false, 2, '', 3, 'a', 'e' * 23 阅读全文

posted @ 2022-09-07 10:31 小馬過河﹎ 阅读(17) 评论(0) 推荐(0)

计算两个或多个数字/数组的总和

摘要: JavaScript const sum = (...arr) => [...arr].reduce((acc, val) => acc + val, 0) Examples sum(1, 2, 3, 4) // 10 sum(...[1, 2, 3, 4]) // 10 阅读全文

posted @ 2022-09-07 10:18 小馬過河﹎ 阅读(26) 评论(0) 推荐(0)

统计数组中某个值出现的次数

摘要: JavaScript const countOccurrences = (arr, val) => arr.reduce((a, v) => (v val ? a + 1 : a), 0) Examples countOccurrences([1, 1, 2, 1, 2, 3], 1) // 3 阅读全文

posted @ 2022-09-07 10:15 小馬過河﹎ 阅读(36) 评论(0) 推荐(0)

获取2个数组的交集

摘要: JavaScript const intersection = (a, b) => { const s = new Set(b) return [...new Set(a)].filter(x => s.has(x)) } Examples intersection([1, 2, 3], [4, 3 阅读全文

posted @ 2022-09-07 10:09 小馬過河﹎ 阅读(19) 评论(0) 推荐(0)

获取数组的第n个元素

摘要: JavaScript const nthElement = (arr, n = 0) => (n -1 ? arr.slice(n) : arr.slice(n, n + 1))[0] Examples nthElement(['a', 'b', 'c'], 1) // 'b' nthElement 阅读全文

posted @ 2022-09-07 10:06 小馬過河﹎ 阅读(33) 评论(0) 推荐(0)

过滤数组中的(多个)指定值

摘要: JavaScript const without = (arr, ...args) => arr.filter(v => !args.includes(v)) Examples without([2, 1, 2, 3], 1, 2) // [3] 阅读全文

posted @ 2022-09-07 09:57 小馬過河﹎ 阅读(19) 评论(0) 推荐(0)

获取数组的前n个元素

摘要: JavaScript const firstN = (arr, n) => arr.slice(0, n) Examples firstN(['a', 'b', 'c', 'd'], 2) // ['a', 'b'] 阅读全文

posted @ 2022-09-07 09:49 小馬過河﹎ 阅读(39) 评论(0) 推荐(0)

获取数组的最后n个元素

摘要: JavaScript const lastN = (arr, n) => arr.slice(-n) Examples lastN(['a', 'b', 'c', 'd'], 2) // ['c', 'd'] 阅读全文

posted @ 2022-09-07 09:44 小馬過河﹎ 阅读(70) 评论(0) 推荐(0)

数组去重

摘要: JavaScript const uniqueElements = arr => [...new Set(arr)] Examples uniqueElements([1, 2, 2, 3, 4, 4, 5]) // [1, 2, 3, 4, 5] 阅读全文

posted @ 2022-09-07 09:37 小馬過河﹎ 阅读(10) 评论(0) 推荐(0)

检查数组中是否有重复值

摘要: JavaScript const hasDuplicates = arr => new Set(arr).size !== arr.length Examples hasDuplicates([0, 1, 1, 2]) // true hasDuplicates([0, 1, 2, 3]) // f 阅读全文

posted @ 2022-09-07 09:34 小馬過河﹎ 阅读(20) 评论(0) 推荐(0)

如果提供的值不是数组,则将其强制转换为数组。

摘要: JavaScript const castArray = val => (Array.isArray(val) ? val : [val]) Examples castArray('foo') // ['foo'] castArray([1]) // [1] 阅读全文

posted @ 2022-09-07 09:25 小馬過河﹎ 阅读(18) 评论(0) 推荐(0)

检查数组是否子集

摘要: JavaScript const includesAll = (big, small) => small.every(s => big.includes(s)) Examples includesAll([1, 2, 3, 4], [1, 4]) // true includesAll([1, 2, 阅读全文

posted @ 2022-09-07 09:22 小馬過河﹎ 阅读(7) 评论(0) 推荐(0)

检查两个数组是否存在交集

摘要: JavaScript const includesAny = (arr1, arr2) => arr2.some(i2 => arr1.includes(i2)) Examples includesAny([1, 2, 3, 4], [2, 9]) // true includesAny([1, 2 阅读全文

posted @ 2022-09-07 09:18 小馬過河﹎ 阅读(16) 评论(0) 推荐(0)

数组去重合并

摘要: JavaScript const union = (a, b) => Array.from(new Set([...a, ...b])) Examples union([1, 2, 3], [4, 3, 2]) // [1, 2, 3, 4] 阅读全文

posted @ 2022-09-07 09:06 小馬過河﹎ 阅读(9) 评论(0) 推荐(0)

loops/reduce/方法链 处理数组对比

摘要: loops 循环 const files = ['foo.txt ', '.bar', ' ', 'baz.foo'] let filePaths = [] for (let file of files) { const fileName = file.trim() if (fileName) { 阅读全文

posted @ 2022-09-07 08:47 小馬過河﹎ 阅读(26) 评论(0) 推荐(0)

导航