Skip to content
Advertisement

How to add a property to every object in a nested JSON structure in JavaScript?

Let’s suppose I have an API response that looks like this:

JavaScript

I want to transform it by adding the rid property to each object.

JavaScript

How can I do so? I think there is some kind of recursion involved but I haven’t been able to draft a solution?

Thanks.

Advertisement

Answer

You just need to iterate through object with recursive function.

Try below snippet.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement