gxvigo
gxvigo

Reputation: 837

Javascript test for existence of object key in a object with arrays

I need to check if a property in a complex object (nested objects with arrays) exists or not. I found several posts on this subject, the most visited the one below. The problem with the provided solution (checkNested function) doesn't work with objects with arrays. Does anyone have a solution that cover this case as well?

Cheers.

javascript test for existence of nested object key

This the function I tested:

function checkProperty(obj, prop) {
  var parts = prop.split('.');
  for (var i = 0, l = parts.length; i < l; i++) {
    var part = parts[i];
    if (obj !== null && typeof obj === "object" && part in obj) {
      obj = obj[part];
    } else {
      return false;
    }
  }
  return true;
}

This is an example of my object:

{
  "_msgid": "3ae30deb.af9962",
  "topic": "",
  "payload": "I am really upset terrible service",
  "error": null,
  "parts": {
    "id": "3ae30deb.af9962",
    "type": "array",
    "count": 2,
    "len": 1,
    "index": 0
  },
  "case_id": "0001",
  "features": {
    "usage": {
      "text_units": 1,
      "text_characters": 34,
      "features": 7
    },
    "sentiment": {
      "document": {
        "score": -0.912124,
        "label": "negative"
      }
    },
    "semantic_roles": [{
      "subject": {
        "text": "I"
      },
      "sentence": "I am really upset terrible service",
      "object": {
        "text": "really upset terrible service",
        "keywords": [{
          "text": "terrible service"
        }]
      },
      "action": {
        "verb": {
          "text": "be",
          "tense": "present"
        },
        "text": "am",
        "normalized": "be"
      }
    }],
    "language": "en",
    "keywords": [{
      "text": "terrible service",
      "sentiment": {
        "score": -0.912124
      },
      "relevance": 0.902721,
      "emotion": {
        "sadness": 0.462285,
        "joy": 0.002207,
        "fear": 0.125395,
        "disgust": 0.17766,
        "anger": 0.575927
      }
    }],
    "entities": [],
    "emotion": {
      "document": {
        "emotion": {
          "sadness": 0.462285,
          "joy": 0.002207,
          "fear": 0.125395,
          "disgust": 0.17766,
          "anger": 0.575927
        }
      }
    },
    "concepts": [],
    "categories": [{
      "score": 0.99946,
      "label": "/health and fitness/disease/headaches and migraines"
    }, {
      "score": 0.0155692,
      "label": "/education/school"
    }, {
      "score": 0.0141217,
      "label": "/family and parenting/children"
    }]
  }
}

And a failure test:

console.log(checkProperty(msg, 'features.keywords[0].text') ? msg.features.keywords[0].text : "NA");

Upvotes: 0

Views: 86

Answers (1)

Nathan Friedly
Nathan Friedly

Reputation: 8146

The checkProperty function you're using doesn't recognize brackets ([ and ]), it only understands dots. So, just give it dots:

checkProperty(msg, 'features.keywords.0.text');

Upvotes: 1

Related Questions