Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // var o = { success: true }
- // Object.setByPath('data.person.name', o, 'Mike')
- // => { success: true, data: { person: { name: 'Mike'} } }
- Object.setByPath = function(path, obj, value) {
- var parts = path.split('.');
- return parts.reduce(function(prev, curr, ix) {
- return (ix + 1 == parts.length)
- ? prev[curr] = value
- : prev[curr] = prev[curr] || {};
- }, obj);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement