简体   繁体   English

过滤JSON数据并附加到div

[英]Filtering JSON data and appending to divs

I have a local JSON file and I'm trying to filter data from it and append that data to divs in my html file. 我有一个本地JSON文件,我正在尝试从中过滤数据并将该数据附加到我的html文件中的div。 I'm unable to see the data in the console, so I'm not sure if I'm doing console.log incorrectly (see below) or if the data is "inaccessible" based on scope. 我无法在控制台中看到数据,所以我不确定我是否正在console.log (见下文),或者根据范围是否“无法访问”数据。

JS file: JS档案:

import $ from 'jquery';
import jsonData from "./test.json";

function _loadDispForm() {
        let dispData = jsonData.d.results.filter(x => {
            return {
                "Title": x.Title,
                "GoalRange": x.GoalRange,
                "Office": x.Office,
                "Role": x.Role,
                "IsFilled": x.IsFilled,
                "Employee": x.Employee,
                "IsActive": x.IsActive,
                "Notes": x.Notes
            }

        })      
        $("#display-form-job-title").append("Title");

        console.log(x.Title);
        console.log(JSON.stringify(dispData));
    }

JSON snippet: JSON片段:

{
  "d": {
    "results": [
      {
        "FileSystemObjectType": 0,
        "Id": 1,
        "Title": "TitleHere",
        "GoalRange": "3",
        "Office": "Somewhere",
        "Role": "FPSL",
        "IsFilled": false,
        "Employee": null,
        "IsActive": true,
        "Notes": null,
        "ID": 1,
        "Attachments": false
...etc

HTML snippet: HTML片段:

<div class="col-6">

  <h3 id="display-form-job-title"></h3>

For the most part, the HTML tags for the other key values are similar to this one. 在大多数情况下,其他键值的HTML标记与此类似。

As other answers have stated, you most likely should be using .map ("morph this length N array to a new length N array"), rather than .filter ("return length <= N array from length N array, including only elements that satisfies the condition"). 正如其他答案所述,你很可能应该使用.map (“将此长度N数组变形为新的长度N数组”),而不是.filter (“从长度为N的数组返回长度<= N数组,仅包括元素满足条件“)。

a = [1,2,3]
console.log(a.map(a => a + 1)) // [2,3,4]
console.log(a.filter(a => a == 2)) // [2]
console.log(a.filter(a => a > 1)) // [2,3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM