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>