Created
March 23, 2017 00:19
-
-
Save gurusubramaniam/a590d5c77cebcb5c6274873506f04058 to your computer and use it in GitHub Desktop.
Gist for iterating through the whole nested object and replace the value with the length of the value. (BOOLEAN will be typecasted to string)
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
| var payload = { | |
| "ArrayObject":[{ | |
| "phone": { | |
| "country_code":"1", | |
| "national_number":"3045277195" | |
| }, | |
| "phone_type":"MOBILE" | |
| }], | |
| "nestedObject" : { | |
| "check" : { | |
| "check" : { | |
| "check" : "Checked" | |
| }, | |
| "checking" :[ | |
| { | |
| "Pleasecheck" : "CHECKED" | |
| }, | |
| { | |
| "Pleasecheck" : "CHECKED" | |
| } | |
| ] | |
| } | |
| }, | |
| "integerArray" : [1,2,3], | |
| "stringArray" : ["one", "two", "three"], | |
| "ID": "asdfasdfasdfsdfsdff" | |
| }; | |
| function iterateObject(object){ | |
| var parsedData = {}; | |
| Object.keys(object).forEach(function(element){ | |
| if(typeof(object[element]) === 'object' && !(object[element] instanceof Array)) { | |
| parsedData[element] = iterateObject(object[element]); | |
| } else if (Array.isArray[object[element]]) { | |
| parsedData[element] = iterateArray(object[element]); | |
| } else { | |
| parsedData[element] = (object[element].toString()).length; | |
| } | |
| }); | |
| return parsedData; | |
| } | |
| function iterateArray(obj, property){ | |
| var arrayObj=[]; | |
| if (obj.length && typeof(obj[0]) !== 'object' && obj[0] !=='') { | |
| return obj.length; | |
| } | |
| obj.map(function(key, index){ | |
| if(typeof(key) === 'object') { | |
| arrayObj.push(iterateObject(key)); | |
| } | |
| }); | |
| return arrayObj; | |
| } | |
| console.log(JSON.stringify(iterateObject(payload))); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment