Update/Create object properties dynamically

梦想与她 提交于 2019-12-06 13:29:36

问题


I'm creating a feature that provides a get() and set() function to read and update values from a JavaScript object. The trick is that I'm using a dot notated string to specify the properties.

I've got the get() working fine, it's the set() that I'm having troubles with.

Sample data:

var data = {
    person: {
        name: 'Fred',
        birthday: '19840101',
        address: {
            street: '123 Main St.',
            city: 'Anytown',
            state: 'NY',
            zip: '123123'
        },
        family: {
            spouse: 'Sally',
            children: ['Sam', 'Frank', 'Susan']
        }
    }
};

get() function:

function get (path) {
    var parts = path.split('.'),
        tmp = data;
    for (var i = 0, l = parts.length; i < l; ++i) {
        if (tmp[parts[i]]) {
            tmp = tmp[parts[i]];
        }
        else {
            tmp = null
            break;
        }
    }
    return tmp;
}

console.log(get('person.name')); // Fred
console.log(get('person.family.children')); // ['Sam', 'Frank', 'Susan']
console.log(get('person.jobs.apple')); // null

set() function:

var foo = null;
function set (path, val) {
    var parts = path.split('.')
        tmp = {},
        foo = data;

    for (var i = 0, l = parts.length; i < l; ++i) {
        if (tmp[parts[i]]) {
            tmp = data[parts[i]];
        }
        else {
            tmp = {};
        }
        if (i+1 === l) {
            tmp = val;
        }
        data[parts[i]] = tmp;
    }
}

set('person.name', 'Dave'); // replace existing name
set('person.family.children', ['Tim', 'Matt', 'Kathy']); // replace existing array
set('person.jobs.apple', 'developer'); // should create the necessary properties
console.log(data);

I end up with the following object:

obj = {
    person: {},
    name: "Dave",
    family: {},
    children: ["Tim","Matt","Kathy"],
    jobs: {},
    apple: "developer"
}

Any thoughts on how to accomplish this?


回答1:


You do not really need any functions at all for this.

See this

var data = {
    person: {
        name: 'Fred',
        birthday: '19840101',
        address: {
            street: '123 Main St.',
            city: 'Anytown',
            state: 'NY',
            zip: '123123'
        },
        family: {
            spouse: 'Sally',
            children: ['Sam', 'Frank', 'Susan']
        }
    }
};

alert(data.person.name);
data['person']['name'] = 'Tim';
alert(data['person']['name']);

There is also this answer which may help

Javascript object key value coding. Dynamically setting a nested value




回答2:


It appears that you have some typos in your set code.

The line tmp = data[parts[i]]; should really be tmp = tmp[parts[i]];. And then you probably want to put that into a new variable - say, newTmp, - so that you can assign it back into the object later:

    tmp[parts[i]] = newTmp; 
    tmp = newTmp;



回答3:


Your problem seems to be the data[parts[i]] = tmp; at the end of the set function.



来源:https://stackoverflow.com/questions/8178357/update-create-object-properties-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!