Javascript 如何检测多级对象具有未定义或空属性?

Javascript 如何检测多级对象具有未定义或空属性?,javascript,lodash,Javascript,Lodash,在我的代码的许多地方,我都有类似于下面的检查。它非常冗长,而且丑陋。有更好的办法吗?仅供参考,我在所有项目中都使用Lodash,因此我可以访问该强大的库 if (myAssessments[orderId].report && myAssessments[orderId].report[categoryProductCode] && myAssessments[orderId].report[categoryProductCode].categor

在我的代码的许多地方,我都有类似于下面的检查。它非常冗长,而且丑陋。有更好的办法吗?仅供参考,我在所有项目中都使用Lodash,因此我可以访问该强大的库

if (myAssessments[orderId].report &&
    myAssessments[orderId].report[categoryProductCode] &&
    myAssessments[orderId].report[categoryProductCode].categories &&
    myAssessments[orderId].report[categoryProductCode].categories[comment.categoryId]) {

    // Do something related to
    // myAssessments[orderId].report[categoryProductCode].categories[comment.categoryId]
}

这不是一种优雅的方式,但你可以穿上试试看

var result;
    try{
    result = myAssessments[orderId].report[categoryProductCode].categories[comment.categoryId]
    }catch{}

if (result){
  // do it
}

这不是一种优雅的方式,但你可以穿上试试看

var result;
    try{
    result = myAssessments[orderId].report[categoryProductCode].categories[comment.categoryId]
    }catch{}

if (result){
  // do it
}

使用内置的isset功能:

if (isset(myAssessments[orderId].report) &&
    isset(myAssessments[orderId].report[categoryProductCode]) &&
    isset(myAssessments[orderId].report[categoryProductCode].categories) &&
    isset(myAssessments[orderId].report[categoryProductCode].categories[comment.categoryId)]) {

使用内置的isset功能:

if (isset(myAssessments[orderId].report) &&
    isset(myAssessments[orderId].report[categoryProductCode]) &&
    isset(myAssessments[orderId].report[categoryProductCode].categories) &&
    isset(myAssessments[orderId].report[categoryProductCode].categories[comment.categoryId)]) {

可以使用具有所有属性的数组进行检查和迭代,直到检查完所有属性

function checkProperties(object, keys) {
    return keys.every(function (key) {
        if (key in object) {
            object = object[key];
            return true;
        }
    });
}

// usage
if (checkProperties(myAssessments, [orderId, 'report', categoryProductCode, 'categories', comment.categoryId])) {
    // Do something related to
    // myAssessments[orderId].report[categoryProductCode].categories[comment.categoryId]
}

可以使用具有所有属性的数组进行检查和迭代,直到检查完所有属性

function checkProperties(object, keys) {
    return keys.every(function (key) {
        if (key in object) {
            object = object[key];
            return true;
        }
    });
}

// usage
if (checkProperties(myAssessments, [orderId, 'report', categoryProductCode, 'categories', comment.categoryId])) {
    // Do something related to
    // myAssessments[orderId].report[categoryProductCode].categories[comment.categoryId]
}

由于您使用lodash,因此可以使用
has
方法:

\uj.has(obj,[orderId,'report',categoryProductCode,'categories',comment.categoryId])


或者使用
get
方法获取对象路径的值:

由于使用了lodash,因此可以使用
has
方法:

\uj.has(obj,[orderId,'report',categoryProductCode,'categories',comment.categoryId])


或者使用
get
方法获取对象路径的值:

我有这个generic函数

function chckForKeyPresence(data, arr, checkLength){
    var currData = data;
    for(var i=0; i<arr.length; i++){
        if(!currData.hasOwnProperty(arr[i]))
            return false;

        currData = currData[arr[i]];
    }

    if(checkLength)
        if(currData.length==0)
            return false;

    return true;
}

我有这个基因功能

function chckForKeyPresence(data, arr, checkLength){
    var currData = data;
    for(var i=0; i<arr.length; i++){
        if(!currData.hasOwnProperty(arr[i]))
            return false;

        currData = currData[arr[i]];
    }

    if(checkLength)
        if(currData.length==0)
            return false;

    return true;
}

您可以为此使用递归。检查是否存在null或未定义。在这里您可以找到一个很好的方法。@Christos我的问题与您链接的问题重复。有人能把我的问题标记为重复的吗?@Blackbird我没有这个特权,但我刚刚投票赞成关闭指定这个链接。你可以使用递归。检查是否存在null或未定义。在这里您可以找到一个很好的方法。@Christos我的问题与您链接的问题重复。有人能把我的问题标记为重复吗?@Blackbird我没有这个特权,但我只是投票赞成关闭指定这个链接。