in this video i have explained how you can traverse a nested JavaScript object with the help of recursion
here is the code
let obj = {
name: "raj",
roll: 892,
id: {
idName: "raj",
},
section: {
sectionName: "raj",
alias: {
name: "raj",
},
},
};
let changeName = (obj) => {
for (let i in obj) {
if (obj[i] == "raj") {
obj[i] == "abhishek";
} else {
changeName(obj[i]);
}
}
};
changeName(obj);
console.log(obj);
result:
{
name: 'raj',
roll: 892,
id: { idName: 'raj' },
section: { sectionName: 'raj', alias: { name: 'raj' } }
}
Top comments (5)
can i get some feedbacks?
I had ever found a case when I thought I needed to solve using recursion. That's why this topic is still interesting and can be useful.
But I am expecting a textual explanation with some codes that we can just copy-paste and try, evaluate in detail, rather than through a video.
100%, hate seeing just videos here, that’s a blogging platform in the end, not a YouTube proxy. So I come here to read and never open videos. That makes providing any feedback impossible unfortunately 🤷♂️
will keep that in mind from next time
will keep that in mind from next time