可以在控制台中看到JSON数据,无法呈现到浏览器

时间:2019-03-07 15:40:51

标签: javascript jquery json function console

我正在从本地文件中提取JSON数据。如果数据是true,那么我想将其附加到div,否则请不要显示。

我可以在true中看到console.log数据,因此现在可以添加它了,但是我的return语句遇到了问题(请参见下面的代码)。有什么想法吗?

JS代码段:

import testjson from './test.json';

    function loadTopCourses() {
        let isTop = testjson.d.results.filter(x => x.TopTrainingCourse === true) {
            return {
                "Title": val.Title
            }
        };

        console.log(isTop)

        let showTopTitles = isTop;

        for (var i = 0; i < showTopTitles.length; i++) {
            let li = $("<li></li>");
            $(li).append(showTopTitles[i].Title);
            $(".top-training-ul").append(li)
        };

    } // ------------------ loadTopCourses

    loadTopCourses();

JSON代码段:

{
   "d": {
     "results": [
       {
         ...
         "Id": 1,
         "Title": "Training 1",
         "Category": "Enter Choice #1",
         "Topic": "Enter Choice #1",
         "Description": "My Test description",
         "TopTrainingCourse": false, // ------------ //
         "ID": 1,
         "Modified": "2019-03-05T20:13:46Z",
         "Created": "2019-03-05T20:13:36Z"
       },
...
...
"FileSystemObjectType": 0,
         "Id": 2,
         "Title": "Training 2",
         "Category": "Enter Choice #2",
         "Topic": "Enter Choice #1",
         "Description": null,
         "TopTrainingCourse": true, // ------------- //
         "ID": 2,
         "Modified": "2019-03-05T20:14:00Z",
         "Created": "2019-03-05T20:13:53Z"
       },
...
...

console.log:

(7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}] // ------ correct # of true values
0: {__metadata: {…}, FirstUniqueAncestorSecurableObject: {…}, RoleAssignments: {…}, AttachmentFiles: {…}, ContentType: {…}, …}
1:
ID: 4
Id: 4
Modified: "2019-03-05T22:33:04Z"
OData__UIVersionString: "1.0"
ParentList: {__deferred: {…}}
RoleAssignments: {__deferred: {…}}
Title: "Training 4"
TopTrainingCourse: true // ------------- //
Topic: "Enter Choice #1"

1 个答案:

答案 0 :(得分:1)

您可以map将过滤的结果放入所需形式的新数组中:

let isTop = testjson.d.results.filter(x => x.TopTrainingCourse === true)
.map(x => { return { Title: x.Title } });