原始地址:https://github.com/30-seconds...
JSONtoCSV
将一个对象数组转换成 CSV 格式的字符串,这个 CSV 只包含给定的列。
const JSONtoCSV = (arr, columns, delimiter = ',') => [ columns.join(delimiter), ...arr.map(obj => columns.reduce( (acc, key) => `${acc}${!acc.length ? '' : delimiter}"${!obj[key] ? '' : obj[key]}"`, '' ) ) ].join('\n');// own understandconst JSONtoCSV = (arr, columns, delimiter = ',') => { // 初始化表头数据 const newArr = [columns.join(delimiter)]; for (const obj of arr) { const str = columns.reduce((acc, key) => { // 是否显示分隔符 const first = acc ? delimiter : ''; // 显示的字符 const sencond = obj[key] ? obj[key] : ''; return `${acc}${first}"${sencond}"` }, ''); newArr.push(str); } return newArr.join('\n');};// exampleJSONtoCSV([{ a: 1, b: 2 }, { a: 3, b: 4, c: 5 }, { a: 6 }, { b: 7 }], ['a', 'b']); // 'a,b\n"1","2"\n"3","4"\n"6",""\n"","7"'JSONtoCSV([{ a: 1, b: 2 }, { a: 3, b: 4, c: 5 }, { a: 6 }, { b: 7 }], ['a', 'b'], ';'); // 'a;b\n"1";"2"\n"3";"4"\n"6";""\n"";"7"'
last
返回数组的最后一个元素。
const last = arr => arr[arr.length - 1];// examplelast([1, 2, 3]); // 3
longestItem
给定一个只由 length
属性的元素组成数组,返回这个数组中长度最大的元素。
使用 Array.prototype.reduce()
对数组循环,对比元素的 length
属性得到长度最大的元素。
const longestItem = (...vals) => vals.reduce((a, x) => (x.length > a.length ? x : a));// examplelongestItem('this', 'is', 'a', 'testcase'); // 'testcase'longestItem(...['a', 'ab', 'abc']); // 'abc'longestItem(...['a', 'ab', 'abc'], 'abcd'); // 'abcd'longestItem([1, 2, 3], [1, 2], [1, 2, 3, 4, 5]); // [1, 2, 3, 4, 5]longestItem([1, 2, 3], 'foobar'); // 'foobar'
mapObject
将给定数组转换成对象,对象的 key
为数组的元素,对象的 value
为数组元素应用了给定函数的结果。
对给定的数组使用 Array.prototype.reduce()
进行循环,对数组中每一个元素应用给定的函数,将元素已经得到的结果赋给结果对象。
const mapObject = (arr, fn) => (a => ( (a = [arr, arr.map(fn)]), a[0].reduce((acc, val, ind) => ((acc[val] = a[1][ind]), acc), {}) ))();// own understandconst mapObject = (arr, fn) => { return arr.reduce((acc, val, ind) => { acc[val] = fn(val); return acc; }, {});};// examplemapObject([1, 2, 3], a => a * a); // { 1: 1, 2: 4, 3: 9 }mapObject([2, 3, 5], a => a * a); // {2: 4, 3: 9, 5: 25}
maxN
返回给定数组的前 n 个较大值,结果为降序数组。
首先使用 Array.prototype.sort()
对数据进行降序排序,然后使用 Array.prototype.slice()
截断排序后的数组,最后得到前 n 个元素组成的数组。
const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n);// examplemaxN([1, 2, 3]); // [3]maxN([1, 2, 3], 2); // [3,2]
minN
返回给定数组的前 n 个较小值,结果为升序数组。
首先使用 Array.prototype.sort()
对数据进行升序排序,然后使用 Array.prototype.slice()
截断排序后的数组,最后得到前 n 个元素组成的数组。
const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);// exampleminN([1, 2, 3]); // [1]minN([1, 2, 3], 2); // [1,2]
none
当数组中所有元素都不通过指定校验函数时返回 true
,否则返回 false
。
使用了 Array.prototype.some()
校验元素字段是否能通过给定的校验函数,原本是只要有一个符合要求就会返回 true
的结果,对结果取反后可以校验出是否所有元素都不通过给定的校验函数,默认使用 Boolean
构造函数作为校验函数。
const none = (arr, fn = Boolean) => !arr.some(fn);// examplenone([0, 1, 3, 0], x => x == 2); // truenone([0, 0, 0]); // true
nthElement
返回一个数组的第 n 个元素。
主要是使用了 Array.prototype.slice()
对数组进行截取而得到想要的结果,正数、负数的索引都可以正常返回指定的结果,主要是截取 n
和 n+1
之间的第一个元素。
const nthElement = (arr, n = 0) => (n === -1 ? arr.slice(n) : arr.slice(n, n + 1))[0];// examplenthElement(['a', 'b', 'c'], 1); // 'b'nthElement(['a', 'b', 'b'], -3); // 'a'
offset
将数组中指定数量的元素移动到数组的末尾。
首先使用 Array.prototype.slice()
截取 offset
到末尾的数组放到新数组的起始位置,然后再截取 0
到 offset
的元素放到新数组的末尾。
const offset = (arr, offset) => [...arr.slice(offset), ...arr.slice(0, offset)];// exampleoffset([1, 2, 3, 4, 5], 2); // [3, 4, 5, 1, 2]offset([1, 2, 3, 4, 5], -2); // [4, 5, 1, 2, 3]
partition
将指定数组的元素拆分成两个数组,其中通过指定校验函数的元素在第一个数组,未通过指定校验函数的元素放在第二个数组。
首先是使用 Array.prototype.reduce()
对数组进行循环,然后对每一个元素应用指定的校验函数,通过校验的第一个结果数组 push
当前元素,未通过校验的第二个结果数组 push
当前元素。
const partition = (arr, fn) => arr.reduce( (acc, val, i, arr) => { acc[fn(val, i, arr) ? 0 : 1].push(val); return acc; }, [[], []] );// examplepartition([1, 2, 3, 4, 5, 6], item => item % 2); // [[1, 3, 5], [2, 4, 6]]