Skip to content

Instantly share code, notes, and snippets.

@yarliganfatih
Created May 21, 2022 18:28
Show Gist options
  • Save yarliganfatih/3dff117260cc2680706443ab348b3638 to your computer and use it in GitHub Desktop.
Save yarliganfatih/3dff117260cc2680706443ab348b3638 to your computer and use it in GitHub Desktop.
Convert JSON Object ( Multi Dimensional ) Values to 1D Array
function ObjToArr(Obj){
if(typeof(Obj)=="object"){
let Arr = Object.keys(Obj).map(function(key) {
return " "+ObjToArr(Obj[key]); //join with comma separator
});
return Arr;
}else{
return Obj;
}
}
//Convert JSON Object ( Multi Dimensional ) Values to 1D Array
let SampleObj = {
"id": 3,
"name": "Clementine Bauch",
"username": "Samantha",
"email": "Nathan@yesenia.net",
"address": {
"street": "Douglas Extension",
"suite": "Suite 847",
"city": "McKenziehaven",
"zipcode": "59590-4157",
"geo": {
"lat": "-68.6102",
"lng": "-47.0653"
}
},
"phone": "1-463-123-4447",
"website": "ramiro.info",
"company": {
"name": "Romaguera-Jacobson",
"catchPhrase": "Face to face bifurcated interface",
"bs": "e-enable strategic applications"
}
};
// call function
let ObjToArrResult = ObjToArr(SampleObj);
let SampleResult = [
" 3",
" Clementine Bauch",
" Samantha",
" Nathan@yesenia.net",
" Douglas Extension, Suite 847, McKenziehaven, 59590-4157, -68.6102, -47.0653",
" 1-463-123-4447",
" ramiro.info",
" Romaguera-Jacobson, Face to face bifurcated interface, e-enable strategic applications"
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment