1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
{ const chunk = (input, size) => { return input.reduce((arr, item, idx) => { return idx % size === 0 ? [...arr, [item]] : [...arr.slice(0, -1), [...arr.slice(-1)[0], item]]; }, []); }; console.log(chunk(['a', 'b', 'c', 'd', 'e', 'f'], 3)) }
{ let arrays = [[1, 2, 3, 4, 5], [5, 2, 10]]; const difference = array => array.reduce((a, b) => a.filter(i => !b.includes(i)))
console.log(difference(arrays)) }
{ const flattenDeep = arr => Array.isArray(arr) ? arr.reduce((a, b) => a.concat(flattenDeep(b)), []) : [arr]
console.log(flattenDeep([1, [[2], [3, 4]], 5])) }
{
const flattenDeep = arr => arr.flatMap(subArray => Array.isArray(subArray) ? flattenDeep(subArray) : subArray)
console.log(flattenDeep([1, [[2], [3, 4]], 5]))
}
{ let arrays = [[1, 2, 3], [101, 2, 1, 10], [2, 1]]; const intersection = array => array.reduce((a, b) => a.filter((c => b.includes(c))))
console.log(intersection(arrays)) }
{ let array = ['one', 'two', 'three'] const grouped = arrary => arrary.reduce((r, v, i, a, k = v.length) => ((r[k] || (r[k] = [])).push(v), r), {})
console.log(grouped(array)) }
{ const keyBy = (array, key) => (array || []).reduce((r, x) => ({ ...r, [key ? x[key] : x] : x}), {}) console.log(keyBy([{ id: 'a1', title: 'abc' }, { id: 'b2', title: 'def' }], 'id')) }
{ const fruits = [ {name:"banana", amount: 2}, {name:"apple", amount: 4}, {name:"pineapple", amount: 2}, {name:"mango", amount: 1} ];
const sortBy = key => (a, b) => (a[key] > b[key] ? 1 : ((b[key] > a[key]) ? -1 : 0)) let sortArr = fruits.concat().sort(sortBy('name')) console.log('sortArr', sortArr)
console.log('sortArr', fruits.concat().sort(sortBy('amount'))) }
{ function debounce(func, wait, immediate) { let timeout = null return function() { let args = arguments let context = this clearTimeout(timeout) timeout = null timeout = setTimeout(function() { if (!immediate) { func.apply(context, args) } }, wait) if (immediate && !timeout) { func.apply(context, args) } } }
const show = () => console.log('scrollTop', document.body.scrollTop || document.documentElement.scrollTop)
}
{ function throttle (func, timeFrame) { let lastTime = 0 return function() { let now = Date.now() if (now - lastTime >= timeFrame) { func() lastTime = now } } } const show = () => console.log('scrollTop', document.body.scrollTop || document.documentElement.scrollTop)
}
{ const isEmpty = obj => [Object, Array].includes((obj || {}).constructor) && !Object.entries((obj || {})).length
console.log(isEmpty(null)) console.log(isEmpty('')) console.log(isEmpty({})) console.log(isEmpty([])) console.log(isEmpty({a: '1'})) console.log(isEmpty(undefined))
}
{ const get = (obj, path, defaultValue = undefined) => { const traval = regexp => String.prototype.split .call(path, regexp) .filter(Boolean) .reduce((res, key) => (res !== null && res !== undefined ? res[key] : res), obj)
const result = traval(/[,[\]]+?/) || traval(/[,[\].]+?/) return result === undefined || result === obj ? defaultValue : result } var object = { a: [{ b: { c: 3 } }] }; var result = get(object, 'a[0].b.c', 1); console.log('result', result) }
|