-1

I have three JSON statements below that I have turned into a JavaScript object. I want to print out the actor, verb and object for each of these statements. I'm having trouble with the object.definition.name because sometimes it will appear in other languages. Because of this and the way I have it coded now, it gives me "undefined" for the second statement because the second statement is fr-FR and not en-US. How can I modify the object part of my loop so it gives me an object no matter what language it is?

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Get Statements 1 demo</title>
        <script src="xapiwrapper.min.js"></script>
    </head>
    <body>
        <p id='demo'></p>
        <script>

            var obj1 =
            {
    "statements": [{
            "verb": {
                "id": "http://adlnet.gov/expapi/verbs/initialized",
                "display": {
                    "en-US": "initialized"
                }
            },
            "version": "1.0.0",
            "timestamp": "2017-05-25T13:01:49.439248+00:00",
            "object": {
                "definition": {
                    "extensions": {
                        "http://example.com": 12
                    },
                    "name": {
                        "en-US": "Change management app"
                    }
                },
                "id": "https://www.avimegiddo.com/wp-admin/admin-ajax.php?action=h5p_embed&id=12",
                "objectType": "Activity"
            },
            "actor": {
                "mbox": "mailto:john.doe@abc.com",
                "name": "John Doe",
                "objectType": "Agent"
            },
            "stored": "2017-05-25T13:01:49.439248+00:00",
            "authority": {
                "mbox": "mailto:tom.creighton.ctr@adlnet.gov",
                "name": "tom",
                "objectType": "Agent"
            },
            "id": "2b03bcd0-11bd-4b43-a256-7e7c8cb259fc"
        },

        {
            "verb": {
                "id": "http://adlnet.gov/expapi/verbs/attempted",
                "display": {
                    "en-US": "attempted"
                }
            },
            "version": "1.0.0",
            "timestamp": "2017-05-25T12:54:52.184309+00:00",
            "object": {
                "definition": {
                    "extensions": {
                        "http://h5p.org/x-api/h5p-local-content-id": 12
                    },
                    "name": {
                        "fr-FR": "le livre"
                    }
                },
                "id": "https://www.avimegiddo.com/wp-admin/admin-ajax.php?action=h5p_embed&id=12",
                "objectType": "Activity"
            },
            "actor": {
                "mbox": "mailto:sally.smith@abc.com",
                "name": "Sally Smith",
                "objectType": "Agent"
            },
            "stored": "2017-05-25T12:54:52.184309+00:00",
            "authority": {
                "mbox": "mailto:megiddo@gmail.com",
                "name": "avimegiddo",
                "objectType": "Agent"
            },
            "context": {
                "contextActivities": {
                    "category": [{
                        "id": "http://h5p.org/libraries/H5P.DragText-1.5",
                        "objectType": "Activity"
                    }],
                    "grouping": [{
                        "definition": {
                            "moreInfo": "https://www.avimegiddo.com/business-emails/",
                            "type": "http://activitystrea.ms/schema/1.0/page",
                            "name": {
                                "en": "How to write business emails: be formal and polite. ESL / EFL Practice Quizzes."
                            }
                        },
                        "id": "https://www.avimegiddo.com/business-emails/"
                    }]
                }
            },
            "id": "83105145-a27c-4bec-bbb0-c1c9f9775930"
        },

        {
            "verb": {
                "id": "http://adlnet.gov/expapi/verbs/attempted",
                "display": {
                    "en-US": "attempted"
                }
            },
            "version": "1.0.0",
            "timestamp": "2017-05-25T13:51:45.976631+00:00",
            "object": {
                "definition": {
                    "extensions": {
                        "http://h5p.org/x-api/h5p-local-content-id": 5,
                        "http://h5p.org/x-api/h5p-subContentId": "bb5c8621-1bcf-4cc7-b1b0-8af2950bffcd"
                    },
                    "name": {
                        "en-US": "Drag the words into the correct boxes\n"
                    }
                },
                "id": "https://www.avimegiddo.com/wp-admin/admin-ajax.php?action=h5p_embed&id=5?subContentId=bb5c8621-1bcf-4cc7-b1b0-8af2950bffcd",
                "objectType": "Activity"
            },
            "actor": {
                "mbox": "mailto:terry.phillips@abc.com",
                "name": "Terry Phillips",
                "objectType": "Agent"
            },
            "stored": "2017-05-25T13:51:45.976631+00:00",
            "authority": {
                "mbox": "mailto:megiddo@gmail.com",
                "name": "avimegiddo",
                "objectType": "Agent"
            },
            "context": {
                "extensions": {
                    "http://id.tincanapi.com/extension/ending-point": 1
                },
                "contextActivities": {
                    "category": [{
                        "id": "http://h5p.org/libraries/H5P.DragText-1.5",
                        "objectType": "Activity"
                    }],
                    "parent": [{
                        "id": "https://www.avimegiddo.com/wp-admin/admin-ajax.php?action=h5p_embed&id=5",
                        "objectType": "Activity"
                    }],
                    "grouping": [{
                        "definition": {
                            "moreInfo": "https://www.avimegiddo.com/2017/04/21/business-emails-short-quizzes/",
                            "type": "http://activitystrea.ms/schema/1.0/page",
                            "name": {
                                "en": "Projects ~ Avi Megiddo"
                            }
                        },
                        "id": "https://www.avimegiddo.com/2017/04/21/business-emails-short-quizzes/"
                    }]
                }
            },
            "id": "7b0582d3-c22d-4818-937e-cc0e5ffbbf18"
        }
    ]
}

            var actorVerbObject = "";
            for (i=0; i<3; i++){
                actorVerbObject += obj1.statements[i].actor.name + ", " + obj1.statements[i].verb.display['en-US'] + ", " + obj1.statements[i].object.definition.name['en-US'] + "<br />";
            }
            document.getElementById('demo').innerHTML = actorVerbObject;

        </script>
    </body>
</html>
JJJ
  • 32,902
  • 20
  • 89
  • 102
John M.
  • 347
  • 1
  • 11
  • Possible duplicate of [how to get the value of the only key-value pair in object](https://stackoverflow.com/questions/24927783/how-to-get-the-value-of-the-only-key-value-pair-in-object) – JJJ May 25 '17 at 15:22
  • Please instead of posting reams of code, click the `<>` and create a [mcve] – mplungjan May 25 '17 at 15:23
  • Object.keys() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys for the win. – Egor Stambakio May 25 '17 at 15:36
  • mplungjan - I clicked your link and I see what you mean. I could've eliminated a lot of the extraneous code from the JSON part. In the future I will strive to do this. Thank you for the feedback. – John M. May 25 '17 at 16:04

1 Answers1

0

Use Object.keys See this fiddle

var actorVerbObject = "";
for (i = 0; i < 3; i++) {
    var displayLangs = Object.keys(obj1.statements[i].verb.display);
    var definitionLangs = Object.keys(obj1.statements[i].object.definition.name);
    actorVerbObject += obj1.statements[i].actor.name + ", " + obj1.statements[i].verb.display[displayLangs[0]] + ", " + obj1.statements[i].object.definition.name[definitionLangs[0]] + "<br />";
}
richbai90
  • 4,994
  • 4
  • 50
  • 85