我需要在javascript中获取/设置嵌套对象,我编写了以下setter,并且正在工作,我需要getter的帮助:假设我有以下对象,我想做类似的事情
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;
};
如果有办法使用 Jquery/lodash 或其他库而不是在对象内部循环,我正在徘徊?我还需要一些关于二传手的帮助。