【问题标题】:Javascript Check Object for empty or null fields and arraysJavascript 检查对象是否为空或空字段和数组
【发布时间】:2018-09-29 04:41:00
【问题描述】:

我有一个包含大量对象和嵌入式数组的数组。我需要遍历整个数组以查看是否有任何内容为空或 null。我的问题是检查数组以及数组是否返回空。我一直在获取对象数组,它们不是 null 或未定义,所以即使长度为 0 也可以添加。到目前为止我得到了什么。

var progressCount = 0;
var progressKeyLength = progressBarCriteria.length;
for (var i = 0; i<progressKeyLength; i++){
  //I can find the arrays here but still not able to check length since they are actually object arrays.
  if(Array.isArray(progressBarCriteria[i])){
    console.log('array' + i);
  }
  if (progressBarCriteria[i] !== null && progressBarCriteria[i] !== ""){
    ++progressCount
  }
}


progressBarCritiria = [
   example1: "",
   example2: "asdasdas",
   example3: 233,
   example4: {asda: 1},
   example5: {asadasda: "asdasdA"},
   example6: "",
   example7: [],
   example8: [1, 12312],
   example9: [{1: "ad"}, {1: 12312}],
]

所以 1、6 和 7 不应该加到我的计数中。

【问题讨论】:

  • 请添加上述数组的示例。
  • "我可以在这里找到数组,但仍然无法检查长度,因为它们实际上是对象数组。"我只是问。什么是对象数组?如果是由对象组成的数组,应该还是可以得到它的长度的。
  • 编辑中添加的示例。
  • 您发布的代码会导致语法错误。您用于将项目放入 progessBarCriteria 的语法不正确。如果您尝试使用对象加载数组,则需要将每个项目括在大括号中,例如 { example1: "something", }
  • progressBarCritiria 不是有效数组。

标签: javascript arrays typescript object


【解决方案1】:

如果您需要检查数组的lengthnull 值,您可以考虑Truthy - Falsy 值如下:

if (Array.isArray(progressBarCriteria[i]) && progressBarCriteria[i].length) {
   // This is an array and is not empty.
}
  • Array.isArray(progressBarCriteria[i]) 检查该值是否为数组。
  • 如果此progressBarCriteria[i].length0,则布尔值将为false,否则为true

【讨论】:

  • 这是我在添加第二部分时遇到的错误:“字符串”类型上不存在属性“长度”|布尔值 |技能[] | {状态:字符串; }[] | {标题:字符串; }[] | {类型:字符串; }[] |...'。类型“真”上不存在属性“长度”。
  • @Kenzo 这看起来像是打字稿错误?您为此使用打字稿还是流式?
  • 我正在使用打字稿。不确定逻辑是否不同。对不起
  • 这只是意味着虽然此解决方案在运行时应该可以正常工作,但您会收到编译时错误(您确实这样做了)。由于您使用索引访问项目,因此打字稿不会自动缩小类型。要解决此问题,请将项目拉入一个单独的变量中,然后在 if 条件中使用它。例如,在 if 之前的行:let current = progressBarCriteria[i];,然后在 if 语句中,使用 current 代替。这将允许打字稿正确推断类型。
  • 添加电流实际上为我解决了这个问题。我可以用它来验证它是否是一个数组,然后检查长度。
【解决方案2】:

您可以使用递归函数来做到这一点。重要的是要注意在 javascript 中数组是对象。所以你需要通过if (typeof arr === 'object' &amp;&amp; !(arr instanceof Array))检查对象。更多信息请查看thisthis

function recursive_array_chekc (arr) {

  //check if arr is an object
  if (typeof arr === 'object' && !(arr instanceof Array)) {
    
    //check if object empty
    if (Object.keys (arr).length === 0) {
    
      //do something if empty
      console.log ('this object is empty');
    
    } else {
    
      //check object properties recursivly
      for (var key in arr)
        if (arr.hasOwnProperty (key))
          recursive_array_chekc (arr[key])
    
    }
  
  } else
  if (Array.isArray (arr)) {
  
    //check if array is empty
    if (arr.length === 0) {
    
      //do something if empty
      console.log ('this array is empty');
    
    } else {
    
      //check array elements recursivly
      for (var i = 0; i < arr.length; i++)
        recursive_array_chekc (arr[i])
    
    }
  
  }   

}

【讨论】:

    【解决方案3】:

    我能够查看这两个答案并想出了这个可行的解决方案。这是使用 Typescript,很抱歉造成混淆。

    for (var i = 0; i<progressKeyLength; i++){
      if (!(progressBarCriteria[i] instanceof Array)){
        if(progressBarCriteria[i] !== null && progressBarCriteria[i] !== "") {
            ++progressCount
        }
      } else {
        let current = progressBarCriteria[i];
        if (Array.isArray(current) && current.length !== 0){
          ++progressCount
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-07
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 2021-05-12
      相关资源
      最近更新 更多