Travis build: 877 [cron]
This commit is contained in:
@ -770,6 +770,21 @@
|
||||
"hash": "9cf10fcda51ff8e1d130b0efa7971549a0d0bb4c24090c5766b82c12e9958604"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "deepMapKeys",
|
||||
"type": "snippetListing",
|
||||
"attributes": {
|
||||
"tags": [
|
||||
"object",
|
||||
"recursion",
|
||||
"advanced"
|
||||
],
|
||||
"archived": false
|
||||
},
|
||||
"meta": {
|
||||
"hash": "e66191a28aba485589b3b06933c13d999962af982b5b7b1b136c10f4d09abd8d"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "defaults",
|
||||
"type": "snippetListing",
|
||||
|
||||
@ -1134,6 +1134,28 @@
|
||||
"hash": "9cf10fcda51ff8e1d130b0efa7971549a0d0bb4c24090c5766b82c12e9958604"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "deepMapKeys",
|
||||
"type": "snippet",
|
||||
"attributes": {
|
||||
"fileName": "deepMapKeys.md",
|
||||
"text": "Deep maps an object keys.\n\nCreates an object with the same values as the provided object and keys generated by running the provided function for each key.\n\nUse `Object.keys(obj)` to iterate over the object's keys. \nUse `Array.prototype.reduce()` to create a new object with the same values and mapped keys using `fn`.",
|
||||
"codeBlocks": {
|
||||
"es6": "const deepMapKeys = (obj, f) =>\n Array.isArray(obj)\n ? obj.map(val => deepMapKeys(val, f))\n : typeof obj === 'object'\n ? Object.keys(obj).reduce((acc, current) => {\n const val = obj[current];\n acc[f(current)] =\n val !== null && typeof val === 'object' ? deepMapKeys(val, f) : (acc[f(current)] = val);\n return acc;\n }, {})\n : obj;",
|
||||
"es5": "function _typeof(obj) { if (typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; }; } return _typeof(obj); }\n\nvar deepMapKeys = function deepMapKeys(obj, f) {\n return Array.isArray(obj) ? obj.map(function (val) {\n return deepMapKeys(val, f);\n }) : _typeof(obj) === 'object' ? Object.keys(obj).reduce(function (acc, current) {\n var val = obj[current];\n acc[f(current)] = val !== null && _typeof(val) === 'object' ? deepMapKeys(val, f) : acc[f(current)] = val;\n return acc;\n }, {}) : obj;\n};",
|
||||
"example": "const obj = {\n foo: '1',\n nested: {\n child: {\n withArray: [\n {\n grandChild: ['hello']\n }\n ]\n }\n }\n};\nconst upperKeysObj = deepMapKeys(obj, key => key.toUpperCase());\n/*\n{\n \"FOO\":\"1\",\n \"NESTED\":{\n \"CHILD\":{\n \"WITHARRAY\":[\n {\n \"GRANDCHILD\":[ 'hello' ]\n }\n ]\n }\n }\n}\n*/"
|
||||
},
|
||||
"tags": [
|
||||
"object",
|
||||
"recursion",
|
||||
"advanced"
|
||||
]
|
||||
},
|
||||
"meta": {
|
||||
"archived": false,
|
||||
"hash": "e66191a28aba485589b3b06933c13d999962af982b5b7b1b136c10f4d09abd8d"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "defaults",
|
||||
"type": "snippet",
|
||||
|
||||
Reference in New Issue
Block a user