循环通过不给我其他语言的对象

时间:2017-05-25 15:20:06

标签: javascript

下面我有三个JSON语句,我已经变成了一个JavaScript对象。我想打印出每个语句的actor,动词和对象。我在使用object.definition.name时遇到问题,因为有时它会出现在其他语言中。由于这个和我现在编码的方式,它给了我" undefined"对于第二个陈述,因为第二个陈述是fr-FR而不是en-US。如何修改循环的对象部分,这样无论用什么语言它都会给我一个对象?

<!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>

1 个答案:

答案 0 :(得分:0)

使用Object.keys请参阅此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 />";
}