Skip to content

Object.setByPath function - #1129

Open
upsolution wants to merge 3 commits into
mootools:masterfrom
upsolution:master
Open

Object.setByPath function#1129
upsolution wants to merge 3 commits into
mootools:masterfrom
upsolution:master

Conversation

@upsolution

Copy link
Copy Markdown

Usage example:

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

@upsolution

Copy link
Copy Markdown
Author

This pull closes #264 and #1074

@piotr-cz

Copy link
Copy Markdown

+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
Copy Markdown

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