Sushi
Sushi

Reputation: 676

How to access to an element that is on an object inside another object?

Here is the "big" array that contains two objects here:

bigArray : [Object, Object]
          0:Object
            id:"1"
            text:"t1"
          1:Object
            id:"2"
            text:"t2"

This what console.log(bigArray) returns.

My question is: How to get the two elements t1 and t2 to verify if one of them is undefined (one of them or both)?

Upvotes: 0

Views: 46

Answers (2)

Swaprks
Swaprks

Reputation: 1553

You can iterate over the array which you are getting and check for the field in the following manner:

var bigArray = [
          {

            id:"1",
            text:"t1"
          },
          {
            id:"2",
            text:"t2"
          }
         ];

            for ( var i = 0; i< bigArray.length; i++ ) {
                alert(bigArray[i].id);
              // this field will be the field agains which you need to check
              if ( typeof bigArray[i].somefield == "undefined" ) {
                alert("its undefined");
              }
            }

Here is the fiddle: https://jsfiddle.net/swaprks/sjmd06rm/1/

Upvotes: 1

Michał Perłakowski
Michał Perłakowski

Reputation: 92531

Use Array.prototype.some():

bigArray.some(x=> typeof x === "undefined")

Upvotes: 2

Related Questions