Skip to content

Instantly share code, notes, and snippets.

@giannispan
Created February 1, 2016 17:12
Show Gist options
  • Save giannispan/d2804f8f0b1801de84f4 to your computer and use it in GitHub Desktop.
Save giannispan/d2804f8f0b1801de84f4 to your computer and use it in GitHub Desktop.
Given an object with an arbitrary number of properties, any number of which may be objects or arrays, replace all values which are strictly equal to the string: "dynamic" with a given string occurring anywhere in that object or a child object or array.
function solution (data, replace) {
//Replace all values of "dynamic" with replace
if (data == null)
return data;
for (var prop in data)
if (data.hasOwnProperty(prop)) {
if (data[prop] == "dynamic")
data[prop] = replace;
}
if(data[prop] instanceof Object || data[prop] instanceof Array) {
var result = solution(data[prop], replace);
}
console.log(data);
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment