Search code examples
javascriptobject-literal

Update/Create object properties dynamically


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?


Solution

  • 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