There is an input structure with rules nested inside other rules. In the rules array, wherever there is a 'data' property, its value has to be changed to "foo"
Sample input objects:
{condition: 'and', rules: [{data: '123'}]}
{condition: 'or', rules: [ { data: '123'}, {condition: 'and', rules: [{data:'123'},{ data:'456'}] }]
Am recursively calling a function to iterate over and if the item has data property, changing its value
My function:
function iterateRules(input) {
input.rules.map(function(item) {
if(_.has(item, "rules")){
this.iterateRules(item); //bug-needs extra check like accepted answer
} else if(_.has(item, “data”)){
return item.data = “foo”;
}
}, this);
return input;
}