简体   繁体   English

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

[英]Check if an element in an array is array or not in javascript/jquery

I have a JSON which contains the following data, when I process it in the javascript, I wanted to go through each element and if it is NULL then remove it, if it is an array I wanted to loop through the sub array and find any null elements inside it and remove. 我有一个包含以下数据的JSON,当我在javascript中处理它时,我想遍历每个元素,如果它为NULL,则将其删除,如果它是一个数组,我想遍历子数组并查找任何清空其中的元素并删除。 I tried the following code, but it is failed to recognize if an element is an array inside array. 我尝试了以下代码,但无法识别元素是否为数组内的数组。

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: 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"
}

EDIT: Modified JSON 编辑:修改后的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
}
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))
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;
}
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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM