Created
February 11, 2019 21:58
-
-
Save javascripto/efa32e507bdb920c16c2a7657f765c64 to your computer and use it in GitHub Desktop.
Some Array and Object Prototypes
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
| const movieCategories = [ | |
| { | |
| name: "Thrillers", | |
| videos : [ | |
| { | |
| "id": 70111470, | |
| "title": "Die Hard", | |
| "boxarts": [ | |
| { width: 150, height: 200, url: "http://cdn-0.nflximg.com/images/2891/DieHard150.jpg" }, | |
| { width: 200, height: 200, url: "http://cdn-0.nflximg.com/images/2891/DieHard200.jpg" } | |
| ], | |
| "url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
| "rating": 4.0, | |
| "bookmark": [] | |
| }, | |
| { | |
| "id": 654356453, | |
| "title": "Bad Boys", | |
| "boxarts": [ | |
| { width: 200, height: 200, url: "http://cdn-0.nflximg.com/images/2891/BadBoys200.jpg" }, | |
| { width: 150, height: 200, url: "http://cdn-0.nflximg.com/images/2891/BadBoys150.jpg" } | |
| ], | |
| "url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
| "rating": 5.0, | |
| "bookmark": [{ id: 432534, time: 65876586 }] | |
| } | |
| ] | |
| }, | |
| { | |
| name: "New Releases", | |
| videos: [ | |
| { | |
| "id": 65432445, | |
| "title": "The Chamber", | |
| "boxarts": [ | |
| { width: 150, height: 200, url: "http://cdn-0.nflximg.com/images/2891/TheChamber150.jpg" }, | |
| { width: 200, height: 200, url: "http://cdn-0.nflximg.com/images/2891/TheChamber200.jpg" } | |
| ], | |
| "url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
| "rating": 4.0, | |
| "bookmark": [] | |
| }, | |
| { | |
| "id": 675465, | |
| "title": "Fracture", | |
| "boxarts": [ | |
| { width: 200, height: 200, url: "http://cdn-0.nflximg.com/images/2891/Fracture200.jpg" }, | |
| { width: 150, height: 200, url: "http://cdn-0.nflximg.com/images/2891/Fracture150.jpg" }, | |
| { width: 300, height: 200, url: "http://cdn-0.nflximg.com/images/2891/Fracture300.jpg" } | |
| ], | |
| "url": "http://api.netflix.com/catalog/titles/movies/70111470", | |
| "rating": 5.0, | |
| "bookmark": [{ id: 432534, time: 65876586 }] | |
| } | |
| ] | |
| } | |
| ]; | |
| module.exports = { | |
| movieCategories | |
| }; | |
| const print = (...args) => console.log(...args); | |
| Object.prototype.z = function(func) { | |
| return func(this.valueOf()) && this.valueOf(); | |
| }; | |
| Array.prototype.concatAll = function() { | |
| return this.reduce((acc, arr) => acc.concat(arr)); | |
| }; | |
| Array.prototype.sum = function() { | |
| return this.reduce((acc, n) => acc + n, 0); | |
| }; | |
| Array.prototype.uniqueValues = function() { | |
| return [...new Set(this)]; | |
| }; | |
| Array.prototype.groupBy = function(field) { | |
| return this.reduce((acc, it) => { | |
| acc[it[field]] = acc[it[field]] + 1 || 1; | |
| return acc; | |
| }, {}); | |
| }; | |
| Array.prototype.indexBy = function(field = 'id') { | |
| return this.reduce((acc, it) => (acc[it[field]] = it) && acc, []); | |
| }; | |
| Object.prototype.reverseKeyValue = function() { | |
| return Object.keys(this) | |
| .reduce( | |
| (acc, k) => (acc[this[k]] = [...(acc[this[k]] || []), k], acc), | |
| {}); | |
| }; | |
| Object.prototype.toQueryString = function() { | |
| return Object.entries(this) | |
| .map(([k, v]) => [k, v].map(encodeURIComponent).join('=')) | |
| .join('&'); | |
| }; | |
| Array.prototype.table = function(...props) { | |
| const self = [...props].length ? JSON.parse(JSON.stringify(this, [...props])) : this; | |
| return [Object.keys(self[0]).map(s => (''+s).toUpperCase()).join('\t')] | |
| .concat(self.map(obj =>Object.values(obj).join('\t'))) | |
| .join('\n'); | |
| } | |
| const users = [ | |
| { id: 11, name: 'Adam', age: 23, group: 'editor' }, | |
| { id: 47, name: 'John', age: 28, group: 'admin' }, | |
| { id: 85, name: 'William', age: 34, group: 'editor' }, | |
| { id: 97, name: 'Oliver', age: 28, group: 'admin' } | |
| ]; | |
| const cities = { | |
| Lyon: 'France', | |
| Berlin: 'Germany', | |
| Paris: 'France' | |
| }; | |
| movieCategories | |
| .map(({ videos }) => videos.map(({ id }) => id)) | |
| .concatAll() | |
| .z(console.log) // [ 70111470, 654356453, 65432445, 675465 ] | |
| [1, 2, 3] | |
| .sum() | |
| .z(console.log); | |
| [1,2,2,2,1,4,1,2,1,5] | |
| .uniqueValues() | |
| .z(console.log); | |
| users | |
| .groupBy('age') | |
| .z(print); | |
| const uTable = users | |
| .indexBy(); | |
| uTable[97].z(print); | |
| cities | |
| .reverseKeyValue() | |
| .z(print) | |
| cities | |
| .toQueryString() | |
| .z(print) | |
| users | |
| .table() | |
| .z(print); | |
| users | |
| .table('id', 'group', 'name') | |
| .z(print); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment