1

I've got an array that has a lot of objects and embedded arrays. I need to iterate through the entire array to see if anything is empty or null. My problem is checking Arrays and whether or not the arrays return empty. I keep getting object arrays and they are not null or undefined so get added in even if length is 0. What I've got so far.

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}],
]

So 1, 6 and 7 should not be added to my count.

Kenzo
  • 347
  • 1
  • 3
  • 18
  • 2
    please add an example of the mentioned array. – Nina Scholz Apr 18 '18 at 20:05
  • "I can find the arrays here but still not able to check length since they are actually object arrays." I am just asking. What is an object array? If it is an array composed of objects, you should still be able to get its length. – t.m. Apr 18 '18 at 20:15
  • Example added up in edit. – Kenzo Apr 18 '18 at 20:22
  • The code you posted would result in a syntax error. The syntax you're using to put items in `progessBarCriteria` is not correct. If you're trying to load the array with objects you'd need to wrap each of those items in curly brackets, for example `{ example1: "something", }` – Tom O. Apr 18 '18 at 20:27
  • `progressBarCritiria` is not a valid array. – Ele Apr 18 '18 at 20:27

3 Answers3

2

If you need to check the length or null value for an array, you can think about the Truthy - Falsy value as follow:

if (Array.isArray(progressBarCriteria[i]) && progressBarCriteria[i].length) {
   // This is an array and is not empty.
}
  • This Array.isArray(progressBarCriteria[i]) checks if that value is an array.
  • If this progressBarCriteria[i].length is 0 the boolean value will be false, ortherwise will be true.
Ele
  • 31,191
  • 6
  • 31
  • 67
  • This is the error I get when adding that 2nd part : Property 'length' does not exist on type 'string | boolean | Skill[] | { status: string; }[] | { title: string; }[] | { type: string; }[] |...'. Property 'length' does not exist on type 'true'. – Kenzo Apr 18 '18 at 20:22
  • @Kenzo That looks like a typescript error? Are you using typescript or flowtype for this? – CRice Apr 18 '18 at 20:24
  • I am using typescript. Wasn't sure if the logic was different. Sorry – Kenzo Apr 18 '18 at 20:25
  • It just means that while this solution should work perfectly fine at runtime, you'll get a compile time error (which you did). Since you're accessing the item with an index, typescript won't automatically narrow the type. To fix, pull the item into a separate variable, then use that in the if condition. EG, on the line before the if: `let current = progressBarCriteria[i];`, then in the if statement, use `current` instead. That will allow typescript to properly infer the type. – CRice Apr 18 '18 at 20:34
  • adding current actually solved it for me. I was able to use this to verify if it was an array or not and then check length. – Kenzo Apr 18 '18 at 20:58
0

You can use a recursive function to do that. It is important to notice that in javascript arrays are objects. So you need to check for objects by if (typeof arr === 'object' && !(arr instanceof Array)). For more informations check this and this.

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])
    
    }
  
  }   

}
wayneOS
  • 1,256
  • 1
  • 9
  • 18
0

I was able to look at both answers and came up with this working solution. This is using Typescript so sorry for confusion.

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
    }
  }
}
Kenzo
  • 347
  • 1
  • 3
  • 18