使用lodash在嵌套对象中生成动态表

时间:2015-07-14 12:45:09

标签: javascript lodash

我有嵌套对象,其中包含学生的详细信息,如下所示:

data = [
            {
                name: 'Gill, Jack',
                subjects: [
                    {
                        name: 'Mathematics',
                        scores: [
                            {
                                year: 7,
                                collection: 1,
                                score: 430,

                            },
                            {
                                year: 7,
                                collection: 2,
                                score: 500,

                            }
                        ]
                    }

} 我将在网页中生成一个动态表格,以显示每个学生的去年标记。但不幸的是它没有工作并打印N / A,我认为lodash有问题,因为我对它很新。我使用静态数组而不是scoreATL,表格具有正确的单元格内容。

//Getting students ID from url
$.get('/rest/report/' + upn, function (data) {
...
data.subjects.forEach(function (subject) {

                html += '<tr>';
                html += '<td>' + subject.name + '</td>';
                for (var j = 0; j < data.cycleNumber; ++j) {


                    var scoreATL = _.findWhere(subject.scores, { year: data.yearForATL, collection: j });

                    if (scoreATL.score)
                        html += '<td>' + scoreATL.score + '</td>';
                    else
                        html += '<td>' + 'N/A' + '</td>';
                }
                html += "</tr>";
            });
...
}

我也尝试过这种方法并在所有细胞中显示N / A:

data.subjects.forEach(function (subject) {
                html += '<tr>';
                html += '<td>' + subject.name + '</td>';
                for (var j = 0; j < data.cycleNumber; ++j) {

                    var scoreATL = 0;
                    scoreATL = _.result(_.findWhere(subject.scores, { year: data.yearForATL, collection: j }), score);

                    if (scoreATL)
                        html += '<td>' + scoreATL + '</td>';
                    else
                        html += '<td>' + 'N/A' + '</td>';
                }
                html += "</tr>";
            });

0 个答案:

没有答案