Created
December 17, 2018 04:15
-
-
Save nathan5x-zz/af4098d0dc00a425ee5d4a10ece918d9 to your computer and use it in GitHub Desktop.
Set a value of property at any deeply nested JavaScript object collection and return the newly created object
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
const data = { | |
foo: { | |
bar: { | |
cc: { | |
str: "Hello I'm deep" | |
}, | |
dd: "Just a level up", | |
}, | |
dd: { | |
cc: "Just another level up" | |
} | |
}, | |
bar: { | |
dd: { | |
foo: { | |
str: "I'm foo string" | |
} | |
} | |
} | |
}; | |
Object.defineProperty(Object, "setProp", { | |
writeable: false, | |
configurable: false, | |
enumerable: false, | |
value: function setProp(collection, keyMap, value) { | |
if (typeof collection !== "object") { | |
return "Collection is not valid"; | |
} | |
if (typeof keyMap !== "object" || !Array.isArray(keyMap)) { | |
return "Key map is not valid"; | |
} | |
let tempResultObj; | |
let firstKey = keyMap[0]; | |
let keyMapLength = keyMap.length; | |
if(keyMapLength === 0 || firstKey === "") { | |
return "Key map is empty"; | |
} | |
let _newCollection = JSON.parse(JSON.stringify(collection)); | |
if (keyMapLength == 1) { | |
_newCollection[firstKey] = value; | |
return _newCollection; | |
} | |
if (!_newCollection.hasOwnProperty(firstKey)) { | |
return "Key not found."; | |
} else { | |
tempResultObj = _newCollection[firstKey]; | |
} | |
for (let i = 1; i < keyMapLength; i++) { | |
let currentKey = keyMap[i]; | |
if (typeof tempResultObj === "object" && | |
tempResultObj.hasOwnProperty(currentKey)) { | |
if (i === keyMapLength - 1) { | |
tempResultObj[currentKey] = value; | |
return _newCollection; | |
} else { | |
tempResultObj = tempResultObj[currentKey]; | |
} | |
} else { | |
return "Key not found"; | |
} | |
} | |
} | |
}); | |
let tc1 = Object.setProp(data, ['foo', 'bar', 'cc', 'str'], 'This is a new string'); | |
let tc2 = Object.setProp(data, ['foo', 'bar','cc'], [25,25,60]); | |
let tc3 = Object.setProp(data, ['foo', 'bar', 'dd'], "Test string"); | |
let tc4 = Object.setProp(data, ['foo', 'bar'], {"fizz": "Fizz fizz", "fuzz": "Fuzz fuzz"}); | |
let tc5 = Object.setProp(data, ['bar', 'foo'], new Date()); | |
let tc6 = Object.setProp(data, ['bar'], "Hello"); | |
let tc7 = Object.setProp(data, ['tt'], "Hello"); | |
let tc8 = Object.setProp(data, [''], new Date()); | |
console.log("Test case: data, ['foo', 'bar', 'cc', 'str'] -->" + JSON.stringify(tc1)); | |
console.log("Test case: data, ['foo', 'bar', 'cc'] -->" + JSON.stringify(tc2)); | |
console.log("Test case: data, ['foo', 'bar', 'dd'] -->" + JSON.stringify(tc3)); | |
console.log("Test case: data, ['foo', 'bar'] -->" + JSON.stringify(tc4)); | |
console.log("Test case: data, ['bar', 'foo'] -->" + JSON.stringify(tc5)); | |
console.log("Test case: data, ['bar'] -->" + JSON.stringify(tc6)); | |
console.log("Test case: data, ['tt'], 'Hello' -->" + JSON.stringify(tc7)); | |
console.log("Test case: data, [''], 'Hello' -->" + JSON.stringify(tc8)); | |
//Console.log output | |
"Test case: data, ['foo', 'bar', 'cc', 'str'] -->{\"foo\":{\"bar\":{\"cc\":{\"str\":\"This is a new string\"},\"dd\":\"Just a level up\"},\"dd\":{\"cc\":\"Just another level up\"}},\"bar\":{\"dd\":{\"foo\":{\"str\":\"I'm foo string\"}}}}" | |
"Test case: data, ['foo', 'bar', 'cc'] -->{\"foo\":{\"bar\":{\"cc\":[25,25,60],\"dd\":\"Just a level up\"},\"dd\":{\"cc\":\"Just another level up\"}},\"bar\":{\"dd\":{\"foo\":{\"str\":\"I'm foo string\"}}}}" | |
"Test case: data, ['foo', 'bar', 'dd'] -->{\"foo\":{\"bar\":{\"cc\":{\"str\":\"Hello I'm deep\"},\"dd\":\"Test string\"},\"dd\":{\"cc\":\"Just another level up\"}},\"bar\":{\"dd\":{\"foo\":{\"str\":\"I'm foo string\"}}}}" | |
"Test case: data, ['foo', 'bar'] -->{\"foo\":{\"bar\":{\"fizz\":\"Fizz fizz\",\"fuzz\":\"Fuzz fuzz\"},\"dd\":{\"cc\":\"Just another level up\"}},\"bar\":{\"dd\":{\"foo\":{\"str\":\"I'm foo string\"}}}}" | |
"Test case: data, ['bar', 'foo'] -->\"Key not found\"" | |
"Test case: data, ['bar'] -->{\"foo\":{\"bar\":{\"cc\":{\"str\":\"Hello I'm deep\"},\"dd\":\"Just a level up\"},\"dd\":{\"cc\":\"Just another level up\"}},\"bar\":\"Hello\"}" | |
"Test case: data, ['tt'], 'Hello' -->{\"foo\":{\"bar\":{\"cc\":{\"str\":\"Hello I'm deep\"},\"dd\":\"Just a level up\"},\"dd\":{\"cc\":\"Just another level up\"}},\"bar\":{\"dd\":{\"foo\":{\"str\":\"I'm foo string\"}}},\"tt\":\"Hello\"}" | |
"Test case: data, [''], 'Hello' -->\"Key map is empty\"" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment