skip to Main Content

How can I find out if a Javascript Object that has a property with an array has been defined?

x = {};
x.gp = [];
x.gp[0] = {};
x.gp[0].jockey = 14;

// Find if the first array within the Object x i.e, x.gp[0] exists NOT just x.gp

Thanks in advance.

2

Answers


  1. I am unsure what you mean with "the first array within the Object". Do you mean the first property that has been set in the object or the first property listed by the [[OwnPropertyKeys]] enumerable ?

    If you just mean "how to know if the object has at least one property whose value is an array", you can do something like this :

    const objWithoutArray = {
      foo: 'far',
      foobar: 'baz'
    }
    
    const objWithArray = {
      foo: 'far',
      foobar: [0, 1, 2, 3]
    }
    
    console.log(Object.values(objWithoutArray).some(Array.isArray)) // <= false
    
    console.log(Object.values(objWithArray).some(Array.isArray)) // <= true

    Basically, we get all the property values with Object.values, and then we run a predicate function with Array.some, and finally we use Array.isArray as the predicate to check if at least one of those values is an array.

    Login or Signup to reply.
  2. It’s not 100% clear what do you want but given xp[0] exists, that means you want to check whether an object contains a non-empty array. So:

    const objWithoutArray = {
      foo: 'far',
      foobar: 'baz'
    }
    
    const objWithArray = {
      foo: 'far',
      foobar: [0, 1, 2, 3]
    }
    
    const hasNonEmptyArray = obj => Object.values(obj).some(val => Array.isArray(val) && val.length);
    
    [objWithoutArray, objWithArray].forEach(obj => console.log(hasNonEmptyArray(obj)));

    If you need some fast code, use an imperative option:

    const objWithoutArray = {
      foo: 'far',
      foobar: 'baz'
    }
    
    const objWithArray = {
      foo: 'far',
      foobar: [0, 1, 2, 3]
    }
    
    const hasNonEmptyArray = obj => {
      for(const key in obj){
        const val = obj[key]
        if(Array.isArray(val) && val.length){
          return true;
        }
      }
      return false;
    };
    
    [objWithoutArray, objWithArray].forEach(obj => console.log(hasNonEmptyArray(obj)));

    And a benchmark:

    enter image description here

    <script benchmark data-count="10000000">
    
    const objWithoutArray = {
      foo: 'far',
      foobar: 'baz'
    }
    
    const objWithArray = {
      foo: 'far',
      foobar: [0, 1, 2, 3]
    }
    
    // @benchmark functional
    
    const hasNonEmptyArray = obj => Object.values(obj).some(val => Array.isArray(val) && val.length);
    
    // @run
    [hasNonEmptyArray(objWithoutArray), hasNonEmptyArray(objWithArray)];
    
    // @benchmark imperative
    
    const hasNonEmptyArray2 = obj => {
      for(const key in obj){
        const val = obj[key]
        if(Array.isArray(val) && val.length){
          return true;
        }
      }
      return false;
    };
    
    // @run
    
    [hasNonEmptyArray2(objWithoutArray), hasNonEmptyArray2(objWithArray)];
    </script>
    <script src="https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js"></script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search