javascript nested object values code example
Example 1: js create nested object from fields
function assign(obj, keyPath, value) {
lastKeyIndex = keyPath.length-1;
for (var i = 0; i < lastKeyIndex; ++ i) {
key = keyPath[i];
if (!(key in obj)){
obj[key] = {}
}
obj = obj[key];
}
obj[keyPath[lastKeyIndex]] = value;
}
Example 2: javascript nested objects
Do my eyes decieve me, or is the second answer the same as the first, but
with more upvotes?