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

标签 javascript jquery json function console

我正在从本地文件中提取 JSON 数据。如果数据为true,那么我想将其附加到 div,如果不是,那么它根本不应该出现。

我可以在 console.log 中看到 true 数据,所以现在需要附加它,但我的 返回遇到了问题 语句(参见下面的代码)。有什么想法吗?

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"
       },
...
...

控制台.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"

最佳答案

您可以将过滤后的结果映射到您想要的形式的新数组中:

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

关于javascript - 可以在控制台中看到 JSON 数据,但无法呈现到浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55047629/

相关文章:

javascript - ngx-datatable 奇怪的行为(行和像素化字体的黑色背景)

c# - 在 ASP.MVC 中,如何在 ViewData 中放置 JavaScript 友好的异常堆栈跟踪?

javascript - 如何检测来自javascript事件的输入更改

javascript - 需要将 GridView 中的链接按钮分组到单个名称下。

ruby-on-rails - 如何在 rails 中使用 like 子句查询?

SQL Server : Transform arrays of JSON objects into table format

javascript - 通过 Javascript 将数据添加到服务器上现有的 JSON 文件

javascript - 如何防止按钮第二次点击事件

javascript - CSS 过渡不动画

jQuery 为元素的左侧设置动画