检查对象javascript中是否存在嵌套属性
我已经回顾了这些问题的一些答案,但是,我想用不同的方式问我的问题 假设我们有一个类似于:“level1.level2.level3…”的字符串,它指示名为Obj的对象中的嵌套属性 关键是我们可能不知道这个字符串中存在多少嵌套属性。例如,它可能是“level1.level2”或“level1.level2.level3.level4” 现在,我想要一个函数,给定Obj和属性字符串作为输入,只需告诉我们对象中是否存在此类嵌套属性(让我们说输出为true或false)检查对象javascript中是否存在嵌套属性,javascript,object,properties,nested,Javascript,Object,Properties,Nested,我已经回顾了这些问题的一些答案,但是,我想用不同的方式问我的问题 假设我们有一个类似于:“level1.level2.level3…”的字符串,它指示名为Obj的对象中的嵌套属性 关键是我们可能不知道这个字符串中存在多少嵌套属性。例如,它可能是“level1.level2”或“level1.level2.level3.level4” 现在,我想要一个函数,给定Obj和属性字符串作为输入,只需告诉我们对象中是否存在此类嵌套属性(让我们说输出为true或false) 更新: 多亏了@Silvinu
更新: 多亏了@Silvinus,我找到了只需稍加修改的解决方案:
private checkNestedProperty(obj, props) {
var splitted = props.split('.');
var temp = obj;
for (var index in splitted) {
if (temp[splitted[index]] === 'undefined' || !temp[splitted[index]]) return false;
temp = temp[splitted[index]];
}
return true;
}
提供问题的基本答案。但需要对其进行调整以处理未定义的情况:
function isDefined(obj, path) {
function index(obj, i) {
return obj && typeof obj === 'object' ? obj[i] : undefined;
}
return path.split(".").reduce(index, obj) !== undefined;
}
您可以使用此功能浏览您的Obj:
var fn = function(obj, props) {
var splited = props.split('.');
var temp = obj;
for(var index in splited) {
if(typeof temp[splited[index]] === 'undefined') return false;
temp = temp[splited[index]]
}
return true
}
var result = fn({ }, "toto.tata");
console.log(result); // false
var result = fn({ toto: { tata: 17 } }, "toto.tata");
console.log(result); // true
var result = fn({ toto: { tata: { tutu: 17 } } }, "toto.foo.tata");
console.log(result); // false
此函数允许探索Obj的嵌套属性,该属性取决于在参数中传递的道具,通过迭代键并检查它是否在给定对象中,您可以使用它和它的thisArg
var fn=函数(o,props){
返回props.split('.').every(k=>k in o&(o=o[k]);
}
console.log(fn({},“toto.tata”);//假的
log(fn({toto:{tata:17},“toto.tata”);//真的
log(fn({toto:{tata:{tututu:17}}},“toto.foo.tata”);//false
基于@Silvinus给出的解决方案,如果您处理嵌套对象中的数组,这里有一个解决方案(数据库查询的结果中经常出现这种情况):
这个答案在案例
fn({a:false},'a')
中失败。次要的一点,但是这里不需要变量temp
。另外,它不应该是temp=temp[splited[index]]代码>?因此,测试用例fn({a:{b:{c:1}},'a.b.c')
导致运行时错误“无法读取未定义的属性'c'。对于第一条注释。。。我同意。我写这段代码太快了。我编辑了它,并通过if(typeof temp[splited[index]]==='undefined')返回false来更改我的测试;第二,对不起,这是个错误。temp=obj[splited[index]]必须被temp=temp[splited[index]]替换@Silvinus谢谢,我已经修改了你的答案(小错误),并在我的更新中加入了正确的函数。@Pedram,你想得到关于每个属性存在的“报告”还是只得到嵌套最多(最后)个属性的“报告”?@RomanPerekhrest事实上,是嵌套最多(最后)个属性。然而,很明显,如果这些属性中的每一个都不存在,那么最后一个属性也不存在:)我想知道为什么我的问题应该得到否决票!为什么要投否决票?唐纳,解释一下怎么样?!或者,也许,你只是点击而没有争论?
checkNested = function(obj, props) {
var splited = props.split('.');
var temp = obj;
for(var index in splited) {
var regExp = /\[([^)]+)\]/;
var matches = regExp.exec(splited[index])
if(matches) {
splited[index] = splited[index].replace(matches[0], '');
}
if(matches) {
if(matches && typeof temp[splited[index]][matches[1]] === 'undefined') return false;
temp = temp[splited[index]][matches[1]];
}
else {
if(!matches && typeof temp[splited[index]] === 'undefined') return false;
temp = temp[splited[index]]
}
}
return true
}
obj = {ok: {ao: [{},{ok: { aa: ''}}]}}
console.log(checkNested(obj, 'ok.ao[1].ok.aa')) // ==> true
console.log(checkNested(obj, 'ok.ao[0].ok.aa')) // ==> false