Created
April 29, 2021 09:26
-
-
Save EileenJuergens/867050025432c14e27346d961d6beb57 to your computer and use it in GitHub Desktop.
JS helper functions and methods
This file contains 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
/* | |
(1) Remove duplicate elements from the array | |
arr = [2,2,2,2,2,3,4,4,4,6] | |
returns: [ 2, 3, 4, 6 ] | |
*/ | |
const allDifferentOptions = arr.filter((item, index) => arr.indexOf(item) === index) | |
const allDifferentOptions = [...new Set(arr)] // alternative | |
/* | |
(2) smallest Integer in array of integers | |
arr = [1,1,1,1,50] | |
returns: 1 | |
*/ | |
const smallestNumber = Math.min(...arr) | |
/* | |
(3) largest Integer in array of integers | |
arr = [1,1,1,1,50] | |
returns: 50 | |
*/ | |
const largestNumber = Math.max(...arr) | |
/* | |
(4) count options of original array | |
arr = ['one', 'two', 'three', 'three', 'three', 'one'] | |
allDifferentOptions = ['one', 'two', 'three'] | |
returns: [ | |
{ itemName: 'one', numberOfOptions: 2 }, | |
{ itemName: 'two', numberOfOptions: 1 }, | |
{ itemName: 'three', numberOfOptions: 3 } | |
] | |
*/ | |
const optionsWithCount = allDifferentOptions.map(item => { | |
const count = arr.filter(el => el === item).length; | |
return { "itemName": item, "numberOfOptions": count } | |
}) | |
/* | |
(5) sort array of objects depending on Integer value | |
arr = [ | |
{ itemName: 'one', numberOfOptions: 2 }, | |
{ itemName: 'two', numberOfOptions: 1 }, | |
{ itemName: 'three', numberOfOptions: 3 } | |
] | |
returns descending (highest to lowest): [ | |
{ itemName: 'three', numberOfOptions: 3 }, | |
{ itemName: 'one', numberOfOptions: 2 }, | |
{ itemName: 'two', numberOfOptions: 1 } | |
] | |
returns ascending (lowest to highest) : [ | |
{ itemName: 'two', numberOfOptions: 1 }, | |
{ itemName: 'one', numberOfOptions: 2 }, | |
{ itemName: 'three', numberOfOptions: 3 } | |
] | |
*/ | |
arr.sort((a, b) => b['numberOfOptions'] - a['numberOfOptions']) // descending | |
arr.sort((a, b) => a['numberOfOptions'] - b['numberOfOptions']) // ascending | |
/* | |
(6) get sum of array of integers | |
arr = [7, 1, 5, 2, -4, 3, 0] | |
returns: 14 | |
*/ | |
arr.reduce((acc, val) => acc + val, 0) | |
/* | |
(7) sum of property values of array of objects | |
arr = [ | |
{ itemName: 'one', numberOfOptions: 2 }, | |
{ itemName: 'two', numberOfOptions: 1 }, | |
{ itemName: 'three', numberOfOptions: 3 } | |
] | |
returns: 6 | |
*/ | |
arr.reduce((acc, val) => acc + (val.numberOfOptions || 0), 0) | |
/* | |
(8) fill array with integers given smallest and largest number | |
smallestNumber = 1 | |
largestNumber = 6 | |
returns: [ 1, 2, 3, 4, 5, 6 ] | |
*/ | |
const arr = []; | |
for (let i = smallestNumber; i <= largestNumber; i++) { | |
arr.push(i); | |
} | |
/* | |
(9) compare two arrays and check (filter for it) if arr1 values are also in arr2 | |
arr1 = [1, 3, 6, 4, 1, 2] | |
arr2 = [ 1, 2, 3, 4, 5, 6 ] | |
returns: [5] | |
*/ | |
arr2.filter(x => !arr1.includes(x)) | |
/* | |
(10) calculated sum of nested object | |
testObj = { | |
1: {name: "test 1", value: 100}, | |
2: {name: "test 2", value: 200}, | |
3: {name: "test 3", value: 300}, | |
4: {name: "test 4", value: 400} | |
} | |
returns: 1000 | |
*/ | |
let sum = 0 | |
function testFunc (obj) { | |
Object.keys(obj).forEach(key => { | |
sum += obj[key].value | |
}) | |
} | |
testFunc(testObj) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment