您可以通过
String
并使用递归函数运行对象:
const checkPath = (o, path) => {
if(path.includes('.')){
if(o.hasOwnProperty(path.split('.')[0])) return checkPath(o[path.split('.')[0]], path.split('.').slice(1).join('.'));
else return false
}else return o.hasOwnProperty(path);
}
像这样使用:
checkPath(Client, 'Details.Title')
let Client = {
ID: 1,
Details: {
Title: 'Dr',
Sex: 'male'
}
};
const checkPath = (o, path) => {
if(path.includes('.')){
if(o.hasOwnProperty(path.split('.')[0])) return checkPath(o[path.split('.')[0]], path.split('.').slice(1).join('.'));
else return false
}else return o.hasOwnProperty(path);
}
console.log(checkPath(Client, 'Details.Title'));
console.log(checkPath(Client, 'Test.Title'));
下面是性感的一句台词:
const checkPath = (o, path) => path.includes('.') ? o.hasOwnProperty(path.split('.')[0]) ? checkPath(o[path.split('.')[0]], path.split('.').slice(1).join('.')) : false : o.hasOwnProperty(path);