Created
February 1, 2016 17:12
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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