0

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");
cнŝdk
32.2k7 gold badges62 silver badges81 bronze badges
asked Nov 2, 2017 at 10:12
4
  • 3
    Can you provide an example that we can work with? Commented Nov 2, 2017 at 10:13
  • you may add the array and the part what you have tried. Commented Nov 2, 2017 at 10:13
  • Post a sample of your object and the code that you tried so far. Commented Nov 2, 2017 at 10:13
  • I edited my question. Thanks Commented Nov 2, 2017 at 10:38

1 Answer 1

1

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');
answered Nov 2, 2017 at 18:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.