Last active
August 29, 2015 14:08
-
-
Save zeusdeux/442352921471d1886554 to your computer and use it in GitHub Desktop.
Convert arbitrary js objects into an array of array of array and so on while keeping the keys close to their values
This file contains 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
function isObject(arg) { | |
return typeof arg === 'object' && arg !== null; | |
} | |
function objToArray(obj){ | |
if (!isObject(obj) || Array.isArray(obj)) throw new SyntaxError('Give me an object to work with plz. kthxbai.'); | |
return output = Object.keys(obj).map(function(key){ | |
var temp = []; | |
temp.push(key); | |
if (isObject(obj[key]) && !Array.isArray(obj[key])) temp.push(objToArray(obj[key])); | |
else temp.push(obj[key]); | |
return temp; | |
}); | |
} | |
//example | |
var d = JSON.parse('{"101":{"value":10,"text":"work! ಠ_ಠ"},"test":{"value":123,"text":"dude what"},"200":{"a":[1,2,3]},"456":{"value":9023,"text":"omg what"},"*_*":{"value":789,"text":"omgwtfbbq you cant be serious"}}'); | |
objToArray(d); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment