Skip to content

Conversation

upsolution
Copy link

Usage example:

var obj = {};
Object.setByPath(obj, 'very.long.path', 'value');
// obj.very.long.path = "value";

@upsolution
Copy link
Author

This pull closes #264 and #1074

@piotr-cz
Copy link

+1 quite helpful with current Object.getFromPath, especially with Hash.Cookie which doesn't work well with nexted objects.

One could use with some tweaking:

var Store = new Hash.Cookie( 'store' );
Store.setByPath( 'state.isNew', true );

@stutteringp0et
Copy link

Why not?:

    Object.extend({
        setFromPath:function(object,paths,value){
            var paths = paths.split('.');
            var traverse = function(context,pathStack){
                var currPath = pathStack.shift();
                if ((typeof context[currPath]) == "object" && pathStack.length){
                    traverse(context[currPath], pathStack);
                }else{
                    context[currPath] = value;
                }
            };
            traverse(object,paths);            
        }
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants