2022年9月7日

获取数组的第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)

导航