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
// example | |
const list = {"you": 100, "me": 75, "foo": 116, "bar": 15}; | |
Object | |
.keys(list) | |
.sort((a, b) => list[a]-list[b]) | |
.reduce((obj, key) => ({ | |
...obj, | |
[key]: list[key] | |
}), {}) | |
// output |
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
// ES6 | |
// Count duplicate items | |
const names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl'] | |
const count = names => | |
names.reduce((a, b) => | |
Object.assign(a, {[b]: (a[b] || 0) + 1}), {}) | |
const duplicates = dict => |
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
// usage example: | |
const myArray = ['a', 1, 'a', 2, '1']; | |
const unique = myArray.filter((v, i, a) => a.indexOf(v) === i); | |
// unique is ['a', 1, 2, '1'] | |
// usage example: | |
const myArray = ['a', 1, 'a', 2, '1']; | |
const unique = [...new Set(myArray)]; | |
// unique is ['a', 1, 2, '1'] |