在javascript / jquery中检查数组中的元素是否为数组

时间:2016-03-28 23:34:45

标签: javascript jquery

我有一个包含以下数据的JSON,当我在javascript中处理它时,我想要遍历每个元素,如果它是NULL然后删除它,如果它是一个数组我想循环通过子数组并在其中找到任何null元素并删除。我尝试了以下代码,但是无法识别元素是否是数组内的数组。

CODE:

            for (var key in data) {

                if (!data[key]) {
                    delete data[key];
                }else if(data.key && data.key.constructor === Array){
                       var subArray = data[key];
                       for(var subKey in subArray){
                          if (!data[key])
                            delete subArray[subKey];
                    }
                }

            }

JSON:

{ 
    confirmDate : "2016-03-27T23:24:36.338Z",
    earliestPossibleInhandDate : "2016-03-28T23:24:36.338Z",
    eventStartTime : null,
    lastChanceDate : null,
    latestPossibleInhandDate : null,
    metas : Array[1],
    onSaleDate : "2016-03-28T23:24:41.461Z"
    primaryCategoryId : "114",
    secondaryGroupings : Array[2],
    status : "active"
}

编辑:修改JSON

{
    "secondaryGroupings": [{
        "groupingId": "720072",
        "status": "active"
    }, {
        "groupingId": null,
        "status": null
    }],
    "secondaryPerformers": [{
        "status": null
    }],
    "metas": [{}],
    "status": "active",
    "primaryCategoryId": "7667",
    "eventStartTime": null,
    "lastChanceDate": null,
    "onSaleDate": "2016-03-29T00:25:56.670Z",
    "confirmDate": "2016-03-28T00:25:56.670Z",
    "earliestPossibleInhandDate": "2016-03-29T00:25:56.670Z",
    "latestPossibleInhandDate": null
}

3 个答案:

答案 0 :(得分:0)

for (var key in data) {
    // you are deleting all the null values
    if (!data[key]) {
        delete data[key];
    }
    // here key is a variable so you must access key as data[key] with bracket notation
    // also the first check here is a bit redundant as you already deleted the null values
    else if(data.key && data.key.constructor === Array){
           var subArray = data[key];
           // you can go over arrays like this but it is better to do a normal for loop or use Array.prototype.forEach because an array is not a key value store
           for(var subKey in subArray){
              if (!data[key])
                delete subArray[subKey];
        }
    }
}

// something like this..
for (var key in data) {
    if (!data[key]) {
        delete data[key];
    }
    if (data[key].constructor === Array){
       var subArray = data[key];
       subArray.forEach(function(item) {
         // I am not sure what your inner array looks like is it an array of objects?
           // do stuff with each item
       })
    }
}

// this works for your dataset but it ain't pretty
function stripData(data) {
  if (Array.isArray(data)){
    data.forEach(function(item) {
       stripData(item);
    })
    return data.filter(function(item) {
      return Object.keys(item).length > 0;
    })
  } else if (typeof data === 'object') {
    for (key in data) {
      if (data[key] === null || data[key].length === 0) delete data[key];
      else data[key] = stripData(data[key]);
    }
    return data;
  }
  for (key in data) {
    if (data[key].length === 0) delete data[key]
  }
  return data;
} 

data = stripData(stripData(data))

答案 1 :(得分:0)

function removeNull(obj,key){
    var thisIsNull=false;
    if(key==null||key==undefined){
        if(isJson(obj)){

            for(var i in obj){
                thisIsNull=removeNull(obj,i);
            }
        }
    }else if(key !=null){
        if(isJson(obj[key])){
            var jsonIsNull=true;
            for(var i in obj[key]){
                if(obj[key][i]!=null){
                    jsonIsNull=false;
                }
                thisIsNull=removeNull(obj[key],i);
            }
            if(jsonIsNull){
                delete obj[key];

                thisIsNull=true;                }
        }else if(obj[key]==null){
            delete obj[key];
        }else if(obj[key] instanceof Array){
            var arrIsNull=true;
            for(var i in obj[key]){
                if(obj[key][i]!=null){
                    arrIsNull=false;
                    thisIsNull=removeNull(obj[key],i);
                }else{
                    delete obj[key][i];
                }
            }
            if(arrIsNull){
                thisIsNull=true;
                delete obj[key];
                thisIsNull=removeNull(obj);
            }
        }


    }
    if(thisIsNull){
        removeNull(obj);
    }
    return thisIsNull;

}

function isJson(obj){
    var isjson = typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length; 
    return isjson;
}

答案 2 :(得分:0)

function deleteNulls(json) {
    for (var key in json) {  
        if (Array.isArray(json[key])) {
            json[key].forEach(function (Obj, index, Array) {
                for (var property in Obj) {
                    if (Obj[property] == null) {
                        Array.splice(index, 1);
                    }
                }
            });
        }
        if (json[key] == null || json[key].length == 0) {    
            delete json[key];
        } 
    } 
}

deleteNulls(myObject);