Last active
December 16, 2021 17:17
-
-
Save gaurangrshah/63a1d64d4641e472d0dc485473b0d012 to your computer and use it in GitHub Desktop.
Synced via Snip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function arrayFill(length, val) { | |
return new Array(length).fill(val); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function flattenObjects(arr) { | |
const object = arr.reduce( | |
(obj, item) => Object.assign(obj, { [item.key]: item.value }), {}); | |
return object | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const flatten = arr => arr.reduce( (acc,item) => Array.isArray(item) ? acc.concat(flatten(item)) : acc.concat(item) , []) | |
/* | |
flatten([ [ [ [1], 2], 3], [4], [], [[5]]]); | |
// -> [1, 2, 3, 4, 5] | |
flatten(['abc', ['def', ['ghi', ['jkl']]]]); | |
// -> ['abc', 'def', 'ghi', 'jkl'] | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @link: https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects | |
export function groupBy(arr, key) { | |
return arr.reduce(function(rv, x) { | |
(rv[x[key]] = rv[x[key]] || []).push(x); | |
return rv; | |
}, {}); | |
}; | |
/* | |
usage: | |
groupBy(['one', 'two', 'three'], 'length'); | |
// => {3: ["one", "two"], 5: ["three"]} | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function limit(arr, c) { | |
return arr.filter((x, i) => { | |
if (i <= c - 1) { | |
return true; | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function mergeObjectsByIndex(arr1, arr2) { | |
return arr1.map((item, i) => ({ ...item, ...arr2[i] })); | |
} | |
// usage: - takes in two arrays of objects of the same length | |
// mergeobjectsByIndex(arr1, arr2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Array Reject | |
* opposite of array.filter, takes a testFn that will remove items that pass the test and returns a new array with the remaining items. | |
* @link: https://dustinpfister.github.io/2020/07/01/lodash_reject/ | |
*/ | |
function reject(arr, testFn) { | |
return arr.filter((item) => !testFn(item)) | |
} | |
// example: | |
let a = [1, 2, 'b', 3]; | |
let test = (el) => typeof el === 'string'; | |
reject(a, test) //=> [1, 2, 3] | |
// compared to typical array.filter: | |
a.filter(test) //=> ['b'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @SCOPE: | |
* Shuffles array in place. ES6 version | |
* @param {Array} an array of [any] items | |
*/ | |
function shuffle(a) { | |
for (let i = a.length - 1; i > 0; i--) { | |
const j = Math.floor(Math.random() * (i + 1)); | |
[a[i], a[j]] = [a[j], a[i]]; | |
} | |
return a; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function sortBy(arrToSort = [], values = ["id", "title"]) { | |
let sorted; | |
if (Array.isArray(values)) { | |
arrToSort.sort( | |
(a, b) => | |
a[values[0]] > b[values[0]] // compare each value individually | |
? 1 // return if true | |
: (a[values[0]] === b[values[0]] // check if equal | |
? a[values[1]] > b[values[1]] // secondary sorting if equal | |
? 1 // return if secondary true | |
: -1 // return if secondary false | |
: -1) - 1 // return if | |
); | |
} | |
sorted = arrToSort.sort((a, b) => (a[values[0]] > b[values[0]] ? 1 : -1)); | |
return sorted; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function uniqueData(data = []) { | |
return [ | |
...data.reduce((map, obj) => map.set(obj.id, obj), new Map()).values(), | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment