简体   繁体   中英

Javascript Setting Accessing nested Objects

I need to get/set nested objects in javascript, I wrote the following setter, and is working, I need help with the getter: Lets say I have the following object, I want to do something like

var prop = get(object, ['complexObj', 'prop1']);
set(object, ['complexObj', 'prop1'], 'newValue');
var object = {
    complexObj: {
        'prop1': 'A'
        'prop2': 'B'},
    'prop3': 'C'
};

// helper function
var read = function(obj, prop) 
{
    if ((obj != null ? obj[prop] : void 0) == null) 
    {
        return;
    }
    return obj[prop];
};
// The following methods allow contextManager data to be accessed either via array of property name parts like ['complexObj', 'subObj', 'subProp1'] 
var get = function (props) {
    var current = properties;
    var val;
    for (var i = 0; i < props.length; i++) {
        if (val = read(current, props[i])) {
            current = val;
        } else {
            return '';
        }
    }
    return current;
};

I was wandering if there is a way to do it using Jquery/lodash or another library, instead looping inside the object? Also I need a little help with the setter.

Try

var object = {
    complexObj: {
        'prop1': 'A',
        'prop2': 'B'
    },
    'prop3': 'C'
};

// helper function
var read = function(obj, prop) {
    return obj ? obj[prop] : undefined;
};
// The following methods allow contextManager data to be accessed either via array of property name parts like ['complexObj', 'subObj', 'subProp1'] 
var get = function (properties, props) {
    var current = properties;
    var val;
    for (var i = 0; i < props.length; i++) {
        if (val = read(current, props[i])) {
            current = val;
        } else {
            return undefined;
        }
    }
    return current;
};
console.log(get(object, ['complexObj', 'prop1']));
//set(object, ['complexObj', 'prop1'], 'newValue');
console.log(get(object, ['complexObj', 'prop1']));

Demo: Fiddle

There were few mistakes like you we calling get before it was defined, and you have to pass the object to get method as its first parameter

// get a property
var get = function(obj, accessor) {
    for (var i = 0, len = accessor.length; i < len; i++) {
        if (!obj) return "";
        obj = obj[accessor[i]];
    }

    return obj;
};

// set a property
var set = function(obj, accessor, value) {
    for (var i = 0, len = accessor.length - 1; i < len; i++) {
        if (!obj) obj = {};
        obj = obj[accessor[i]] = obj[accessor[i]] || {};
    }

    obj[accessor[i]] = value;
};

// example
var object = {
    a: 2,
    b: {
        c: 1
    }
};

get(object, ["b", "c"]); // 1
set(object, ["d"], 3); // { a: 2, b: { c: 1 }, d: 3 }
set(object, ["e", "f"], 4); // { ..., e: { f: 4 } }

/*
object == {
    a: 2,
    b: {
        c: 1
    },
    d: 3,
    e: {
        f: 4
    }
}
*/

This code automatically creates new objects when setting a property that isn't there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM