如果我有对象的引用:
var test = {};
可能(但不是立即)具有嵌套对象,例如:
{level1: {level2: {level3: "level3"}}};
检查深度嵌套对象中是否存在属性的最佳方法是什么?
警报(测试级别1);生成未定义,但警告(test.level1.level2.level3);失败。
我目前正在做这样的事情:
if(test.level1 && test.level1.level2 && test.level1.level2.level3) {
alert(test.level1.level2.level3);
}
但我想知道是否有更好的方法。
使现代化
看起来lodash已经为您的所有嵌套属性获取需求添加了_.get。
_.get(countries, 'greece.sparta.playwright')
https://lodash.com/docs#get
上一个答案
lodash用户可能喜欢lodash.contrib,它有几种方法可以缓解这个问题。
获取路径
签名:_.getPath(obj:Object,ks:String|Array)
基于所描述的路径获取嵌套对象中任何深度处的值给出的钥匙。键可以以数组或点分隔字符串的形式给出。如果无法到达路径,则返回undefined。
var countries = {
greece: {
athens: {
playwright: "Sophocles"
}
}
}
};
_.getPath(countries, "greece.athens.playwright");
// => "Sophocles"
_.getPath(countries, "greece.sparta.playwright");
// => undefined
_.getPath(countries, ["greece", "athens", "playwright"]);
// => "Sophocles"
_.getPath(countries, ["greece", "sparta", "playwright"]);
// => undefined
一个简短的ES5版本@CMS的优秀答案:
// Check the obj has the keys in the order mentioned. Used for checking JSON results.
var checkObjHasKeys = function(obj, keys) {
var success = true;
keys.forEach( function(key) {
if ( ! obj.hasOwnProperty(key)) {
success = false;
}
obj = obj[key];
})
return success;
}
通过类似测试:
var test = { level1:{level2:{level3:'result'}}};
utils.checkObjHasKeys(test, ['level1', 'level2', 'level3']); // true
utils.checkObjHasKeys(test, ['level1', 'level2', 'foo']); // false
function getValue(base, strValue) {
if(base == null) return;
let currentKey = base;
const keys = strValue.split(".");
let parts;
for(let i=1; i < keys.length; i++) {
parts = keys[i].split("[");
if(parts == null || parts[0] == null) return;
let idx;
if(parts.length > 1) { // if array
idx = parseInt(parts[1].split("]")[0]);
currentKey = currentKey[parts[0]][idx];
} else {
currentKey = currentKey[parts[0]];
}
if(currentKey == null) return;
}
return currentKey;
}
如果结果在嵌套或值本身的任何地方失败,则调用函数返回undefined
const a = {
b: {
c: [
{
d: 25
}
]
}
}
console.log(getValue(a, 'a.b.c[1].d'))
// output
25
var a;
a = {
b: {
c: 'd'
}
};
function isset (fn) {
var value;
try {
value = fn();
} catch (e) {
value = undefined;
} finally {
return value !== undefined;
}
};
// ES5
console.log(
isset(function () { return a.b.c; }),
isset(function () { return a.b.c.d.e.f; })
);
如果您在ES6环境中编码(或使用6to5),则可以利用箭头函数语法:
// ES6 using the arrow function
console.log(
isset(() => a.b.c),
isset(() => a.b.c.d.e.f)
);
关于性能,使用try没有性能惩罚。。如果设置了属性,则捕获块。如果属性未设置,则会影响性能。
考虑简单地使用_.has:
var object = { 'a': { 'b': { 'c': 3 } } };
_.has(object, 'a');
// → true
_.has(object, 'a.b.c');
// → true
_.has(object, ['a', 'b', 'c']);
// → true