Flatten a nested object that contains arrays and objects as children. The task is to make the object flat, i.e. there should be no nested children left and everything should be on a single level.
const response = {
name: 'Manu',
age: 21,
characteristics: {
height: '6 feet',
},
techStack: {
language: 'Javascript',
framework: {
name: 'Nextjs',
version: '12',
},
},
};
response = {
age: 21,
characteristics.height: "6 feet",
name: "Manu",
techStack.framework.name: "Nextjs",
techStack.framework.version: "12",
techStack.language: "Javascript"
}
Click to reveal
Recursion is your best friend. :)
Click to reveal
Try checking the typeof
element you're currently at. If it is an array, you might want to recurse.