const myNewobj = {
a: "bangladesh",
b: {
c: "pakistan",
d: {
e: "nepal",
f: {
g: "india",
},
},
},
};
function getValuesOfNextedObject(obj) {
let result = [];
for (let elt in obj) {
if (typeof obj[elt] == "object") {
result = result.concat(getValuesOfNextedObject(obj[elt]));
} else {
result.push([elt, obj[elt]]);
}
}
return result;
}
var res = getValuesOfNextedObject(myNewobj)
console.log(res)
var resultObject = {};
for (let elt of res) {
resultObject[elt[0]] = elt[1];
}
console.log(resultObject);
Output:
{ a: 'bangladesh', c: 'pakistan', e: 'nepal', g: 'india' }
No comments:
Post a Comment