检查数组的null或undefined值?

时间:2016-10-27 05:00:28

标签: javascript arrays node.js express node-async

我有一个以下格式的数组,我必须检查是否有任何null或未定义的值

[{
         "categoryName": "A",
         "product": [{
          "catogoryName": "A",
          "price": "10",
          "productName": "a"
         }, {
          "catogoryName": "A",
          "price": "100",
          "productName": "a2"
         }, {
          "catogoryName": "A",
          "price": "5654",
          "productName": "dfhdh"
         }, {
          "catogoryName": "A",
          "price": "572",
          "productName": "d"
         }]
        }, {
         "categoryName": "B",
         "product": [{
          "catogoryName": "B",
          "price": "10",
          "productName": "b"
         }, {
          "catogoryName": "B",
          "price": "300",
          "productName": "b2"
         }]
        }]

我尝试使用以下代码,但它总是给我success:1消息。

 var validateCategoryList = function (categoryList,callback) {
        async.eachSeries(categoryList, function (categoryChunk, callback) {
            var categoryName = categoryChunk.categoryName;
            var productList = categoryChunk.product;
            console.log(categoryName);
            if(!(categoryName == null || categoryName == undefined)){
                async.eachSeries(productList, function (item, callback) {
                    var productName = item.productName;
                    var price = item.price;
                    console.log(productName);
                    console.log(price);
                    if((!(productName == null || price == undefined)) || (!(productName == null || price == undefined))){
                        callback(null);
                    }
                    else{
                        callback({'success':'0','result':{},'errorMessage':'CategoryLIst invalid'})
                        return;
                    }
                },function (err) {
                    callback(null);
                });
            }
            else{
                callback({'success':'0','result':{},'errorMessage':'CategoryLIst invalid'})
                return;
            }
        },function (err) {
            callback({'success':'1'})
        });

    }

1 个答案:

答案 0 :(得分:2)

使用addLegend() callback作为穷人的方式来遍历对象:

JSON.stringify