上下文:我正在为我的应用程序的设置编写一个Redux reducer(虽然这个问题不是Redux特定的),这是一个嵌套对象。我想使用动态提供的属性名修改设置对象。
示例:
const settings = {
service: {
username: 'TEST',
password: ''
}
}
// Normally this would be passed by Redux, but for the purposes of this exercise it's hardcoded
const settingKey = 'service.username';
console.log(settings[settingKey]); // undefined
console.log(eval(`settings.${settingKey}`)); // works, but bad
我可以考虑在不使用eval的情况下访问子对象的唯一方法是使用正则表达式将settingKey
拆分为其组成部分:
const match = /(.+)\.(.+)/.exec(settingKey);
console.log(settings[match[1]][match[2]];
const settings = {
service: {
username: 'TEST',
password: ''
}
}
const settingKey = 'service.username';
const match = /(.+)\.(.+)/.exec(settingKey);
console.log(settings[match[1]][match[2]]);
这有效,但
有没有办法使用动态名称访问嵌套对象的属性而不使用正则表达式或eval?
答案 0 :(得分:3)
你可以这样做,
var settings = {service: {username: 'TEST', password: ''}}
var key = "service.username";
function getValue(obj, keys){
keys.split(".").forEach(function(itm){
obj = obj[itm];
});
return obj;
}
getValue(settings, key); //"TEST"
或者您只需使用Array#reduce
,
var settings = {service: {username: 'TEST', password: ''}}
var key = "service.username", result = key.split(".").reduce((a,b) => a[b], settings);
console.log(result); // "TEST"
答案 1 :(得分:0)
另一个不使用eval
并使用嵌套属性的选项。
var settings = {service: {username: 'TEST', password: ''}}
var key = "service.username";
console.log(Function('setting', 'return settings.' + key)(settings));

答案 2 :(得分:-1)
我想你只需改变一点:
const settings = {
service: {
username: 'TEST',
password: ''
}
}
console.log(settings['service'].username);