diff --git a/snippet_data/snippets.json b/snippet_data/snippets.json index c36bdb0c2..e981fdfe1 100644 --- a/snippet_data/snippets.json +++ b/snippet_data/snippets.json @@ -1434,9 +1434,9 @@ "type": "snippet", "attributes": { "fileName": "deepMapKeys.md", - "text": "Deep maps an object's keys.\n\nCreates an object with the same values as the provided object and keys generated by running the provided function for each key.\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`.\n\n", + "text": "Deep maps an object's keys.\n\nCreates an object with the same values as the provided object and keys generated by running the provided function for each key.\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`.\n\n", "codeBlocks": { - "code": "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;", + "code": "const deepMapKeys = (obj, fn) =>\n Array.isArray(obj)\n ? obj.map(val => deepMapKeys(val, fn))\n : typeof obj === 'object'\n ? Object.keys(obj).reduce((acc, current) => {\n const key = fn(current);\n const val = obj[current];\n acc[key] =\n val !== null && typeof val === 'object' ? deepMapKeys(val, fn) : val;\n return acc;\n }, {})\n : obj;", "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": [ @@ -1446,10 +1446,10 @@ ] }, "meta": { - "hash": "a4e1e33c0688dbf1ca231d9d8ea315ffed93b7f83f5d8cbf0714f10fdfeda8cf", + "hash": "49d6a6279af3abfd3e63af21edaabc7137bef4b729102c08d333c8a423591edc", "firstSeen": "1544374334", - "lastUpdated": "1587068503", - "updateCount": 102, + "lastUpdated": "1587369956", + "updateCount": 103, "authorCount": 4 } },