Created
January 21, 2018 17:17
-
-
Save ivan-stepaniuk/5d36b8548526c13e80812ca6ea1ddb42 to your computer and use it in GitHub Desktop.
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
| // 1 | |
| function arrayWithRanges(array) { | |
| if (!array || !Array.isArray(array)) return; | |
| let num = [...array]; | |
| let k = 0; | |
| let lastElement = num.length - 1; | |
| for (var i = lastElement; i >= 0; i--) { | |
| if (num[i] == num[i - 1] + 1) { | |
| k++; | |
| continue; | |
| } | |
| if (k > 0) { | |
| let str = num[i] >= 0 ? num[i] : `(${num[i]})`; | |
| str += '-'; | |
| str += num[i + k] >= 0 ? num[i + k] : `(${num[i + k]})`; | |
| num.splice(i, k + 1, str) | |
| } | |
| k = 0; | |
| } | |
| return num; | |
| } | |
| // 2 | |
| function transformObject(obj1) { | |
| obj2 = createFlatObject(obj1); | |
| for (key in obj1) { | |
| if (obj1.hasOwnProperty(key)) { | |
| delete obj1[key] | |
| } | |
| } | |
| Object.assign(obj1, obj2); | |
| } | |
| function createFlatObject(object, newKey, val, result) { | |
| if (!result) result = {}; | |
| if (!newKey) newKey = ''; | |
| if (val) { | |
| result[newKey] = val; | |
| return; | |
| } | |
| for (key in object) { | |
| if (object[key] == null) object[key] = 'null'; | |
| if (typeof object[key] == 'object' && object.hasOwnProperty(key)) { | |
| createFlatObject(object[key], newKey + key, null, result); | |
| } | |
| else { | |
| createFlatObject(object, newKey + key, object[key], result); | |
| } | |
| } | |
| return result; | |
| } | |
| // ------------------------------------------------------------------------------- | |
| // Test | |
| // 1 | |
| const innerArray = [-10, -9, -8, -5, -3, -2, 0, 1, 2, 3, 4, 5, 8, 9, 11, 13, 15, 18, 22, 25, 28, 29, 30]; | |
| console.log(arrayWithRanges(innerArray)); | |
| // 2 | |
| const multidimensionalObject = { | |
| "User": 1, | |
| "Data": { | |
| "FirstName": "Anonimoys", | |
| "LastName": "AnonimoysLastName", | |
| "MiddleName": "AnonimoysMiddleName", | |
| "Role": [ | |
| 1, 2, 4, { | |
| "isOwner": true | |
| }, | |
| { | |
| "hidden": null | |
| } | |
| ] | |
| }, | |
| "Profile": [ | |
| { | |
| "Check": true, | |
| "CheckRole": [ | |
| 1, 2, 34 | |
| ] | |
| }, | |
| { | |
| "Settings": { | |
| "Rider": [ | |
| 1, 2, 3, 4 | |
| ], | |
| "Inside": { | |
| "In": true, | |
| "Out": null | |
| } | |
| } | |
| } | |
| ] | |
| }; | |
| transformObject(multidimensionalObject); | |
| console.log(multidimensionalObject); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment