-1

I am attempting to check if the calendar and tpoint keys have values associated with them.

At first I tried to just check to see if calendar and tpoint keys were included, but then I realized they always would be as they are keys. The reason I am wanting to check to see if the calendar and tpoint keys have values because sometimes they won't.

My attempt is below the packageContents object below.

Does anyone know how I can check if the keys have values?

var packageContents = {
        'packages': [
            {
                'price': '32',
                'name': 'Gold Bundle Package',
                'calendar': {
                    'type': '2year',
                    'color': 'Brushed Nickel',
                },
                'tpoint': {
                    'type': 'Gold',
                    'touches': '21',
                    'years': '7',
                }
            },
            {
                'price': '23',
                'name': 'Bronze Bundle Package',
                'calendar': {
                    'type': '2year',
                    'color': 'Brushed Nickel',
                },
                'tpoint': {
                    'type': 'Bronze',
                    'touches': '9',
                    'years': '7',
                }
            }
        ]
    };

packageContents['packages'].forEach(function (bun) {
    if (bun['calendar'].length >= 1 && bun['tpoint'].length >= 1) {
        var bundleSet;
        console.log(bundleSet);
        console.log('Working');
    }
});

Edit:

var packageContents = {
        'packages': [
            {
                'price': '23',
                'name': 'Bronze Bundle Package',
                'calendar': {
                    'type': '2year',
                    'color': 'Brushed Nickel',
                },
                'tpoint': {
                    'type': '',
                    'touches': '',
                    'years': '',
                }
            }
        ]
    };




var bundleSet = '';

    packageContents['packages'].forEach(function (obj) {
        if (typeof obj.calendar !== 'undefined' && typeof obj.tpoint !== 'undefined') {
            bundleSet = "Bundle";

        }
        else {
            bundleSet = "not bundle"
        }
        console.log(bundleSet);
    });
Paul
  • 3,348
  • 5
  • 32
  • 76

1 Answers1

3

Your .forEach() loop is looping over the objects in the packageContent.packages array, so it's a good idea to use a variable name to remind you of that.

Then, since you know the static names of the keys in those objects, you don't need to pass them as strings using bracket notation ([]) to the object, you can simply use dot notation on the instance.

Lastly, you just want to check to see if there are values stored in those keys, so a check to see if the type of data stored there is not undefined would do.

var packageContents = {
        'packages': [
            {
                'price': '32',
                'name': 'Gold Bundle Package',
                'calendar': {
                    'type': '2year',
                    'color': 'Brushed Nickel',
                },
                'tpoint': {
                    'type': 'Gold',
                    'touches': '21',
                    'years': '7',
                }
            },
            {
                'price': '23',
                'name': 'Bronze Bundle Package',
                'calendar': {
                    'type': '2year',
                    'color': 'Brushed Nickel',
                },
                'tpoint': {
                    'type': '',
                    'touches': '',
                    'years': '',
                }
            }
        ]
    };

var bundleSet =  null;
packageContents.packages.forEach(function (obj) {

  // We are now looping over each top-level object in the main array.
  // To see if each of the properties in quesiton of those objects have values,
  // we need to loop over those properties
  for(var prop in obj){
    
    // We're only interested in "calendar" and "tpoint"
    if(prop === "calendar" || prop === "tpoint"){
      
      // These properties store objects of their own and it is these properties we need to check
      for(var subProp in obj[prop]){
    
        // Since you know the key names, you can access them directly on the instance 
        // and simply see if they have values by checking that they are not undefined
        if (obj[prop][subProp] !== "") {
          bundleSet = true;
        } else {
          bundleSet = false;
        }
      }
    }

  }
  console.log(bundleSet);
});
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • Thanks! So typeof is just returning undefined or defined? – Paul Jun 30 '17 at 17:10
  • @Paul `typeof` will return the JavaScript type (`"object"`, `"string"`, `"number"`, `"boolean"`, `"function"`, etc.) or it will return `"undefined"` when there is no value. – Scott Marcus Jun 30 '17 at 17:12
  • So why is it showing undefined then? All of the keys have values? Just confused. – Paul Jun 30 '17 at 17:15
  • @Paul Because, in your code, you have `var bundleSet` declared, but you never initialize it to anything and then you immediately log it. That part has nothing to do with the actual problem or solution, It's just extra code that I assumed you had some purpose for. – Scott Marcus Jun 30 '17 at 17:16
  • Ok, so just put `var bundleSet = '';` above the function and then replaced `var bundleSet` as just `bundleSet` in the function. Now the `console.log` doesn't show. Sorry, just trying to get `bundleSet` to accurately declare if they are set or not. – Paul Jun 30 '17 at 17:20
  • @Paul Can you be clearer? What is `bundleSet` supposed to represent and what value is it supposed to have when both properties have values? – Scott Marcus Jun 30 '17 at 17:22
  • @Paul See updated answer. I have modified the second object so that it does not have a `tpoint` key now and you can see that your logged output now shows `true` and `false` because the first object has both keys and the second object does not. – Scott Marcus Jun 30 '17 at 17:27
  • I updated my question with the array showing the values of `tpoint` not set. So, my `console.log()` should be firing in my `else` part of the if statement. – Paul Jun 30 '17 at 17:28
  • 1
    @Paul I misunderstood exactly what you were after and I've updated my answer to reflect your question. in your case, if you expect that the properties may sometimes contain empty strings (i.e. `'years': ''`), then a simple test for an empty sting is all that's needed. – Scott Marcus Jun 30 '17 at 17:43
  • No need to answer the question above... was just talking out loud. You have helped a lot. Thanks for all the help! – Paul Jun 30 '17 at 17:52