簡體   English   中英

可以在控制台中看到JSON數據,無法呈現到瀏覽器

[英]Can see JSON data in console, can't render to browser

我正在從本地文件中提取JSON數據。 如果數據為true那么我想將其附加到div上,如果不是,那么它根本就不會出現。

我可以在console.log看到true數據,因此現在只需添加它即可,但是我的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"

您可以將過濾后的結果map到所需表單的新數組中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM