如何显示/区分json响应中的数据

时间:2017-09-26 17:37:52

标签: json typescript for-loop datatables iteration

我在JSON中有以下动态响应,所以我有一个列表对象,我想使用form_id显示和区分数据。请告诉我如何实现这一目标......

//list object
formsSummery: any = [];

this.formsSummery = (data[0] as any).data_points;

   [ {
  "job" : 10,
  "data_points" : [ {
    "user_id" : 11,
    "ingest_time" : "2017-09-26T13:17:33.489Z",
    "form_id" : 2,
    "form_revision" : 1,
    "device_id" : 0,
    "field1" : "2.2",
    "field2" : "two",
    "field3" : "2",
    "field4" : "1"
  }, {
    "user_id" : 11,
    "ingest_time" : "2017-09-26T13:03:56.757Z",
    "form_id" : 7,
    "form_revision" : 1,
    "device_id" : 0,
    "field1" : "thirteen",
    "field2" : "fourteen",
    "field3" : "fifteen",
    "field4" : "sixteen"
  }, {
    "user_id" : 11,
    "ingest_time" : "2017-09-27T11:58:59.735Z",
    "form_id" : 7,
    "form_revision" : 1,
    "device_id" : 0,
    "field1" : "17",
    "field2" : "TWENTY",
    "field3" : "19",
    "field4" : "1",
    "field5master" : "EIGHTEEN",
    "field6" : "TRUE"
  }, {
    "user_id" : 11,
    "ingest_time" : "2017-09-26T07:23:26.468Z",
    "form_id" : 7,
    "form_revision" : 1,
    "device_id" : 0,
    "field1" : "five",
    "field2" : "seven",
    "field3" : "six",
    "field4" : "eight"
  }, {
    "user_id" : 11,
    "ingest_time" : "2017-09-26T07:24:14.729Z",
    "form_id" : 7,
    "form_revision" : 1,
    "device_id" : 0,
    "field1" : "nine",
    "field2" : "eleven",
    "field3" : "ten",
    "field4" : "twele"
  }, {
    "user_id" : 11,
    "ingest_time" : "2017-09-26T08:29:36.728Z",
    "form_id" : 2,
    "form_revision" : 1,
    "device_id" : 0,
    "field1" : "2.1",
    "field2" : "one",
    "field3" : "1",
    "field4" : "true"
  }, {
    "user_id" : 11,
    "ingest_time" : "2017-09-26T07:18:10.401Z",
    "form_id" : 7,
    "form_revision" : 1,
    "device_id" : 0,
    "field1" : "one",
    "field2" : "three",
    "field3" : "two",
    "field4" : "four"
  }, {
    "user_id" : 11,
    "ingest_time" : "2017-09-27T11:57:06.188Z",
    "form_id" : 2,
    "form_revision" : 1,
    "device_id" : 0,
    "field1" : "2.3",
    "field2" : "THREE",
    "field3" : "3",
    "field4" : "TRUE"
  } ]
} ]

我想使用form_id显示数据。 我想在表中使用form_id(一个表中的form_d = 7的所有数组)显示表中的数据,以及一些表=表单ID的数量。

2 个答案:

答案 0 :(得分:0)

我建议您首先订购data_points数组,例如post。然后使用Set跟踪数组中的form_id's。在您进行过滤时,如果您form_id中已有Set,则会忽略它。

示例:

let x = new Set();
const y = this.formsSummery[0].data_points.filter(z => {
  return !x.has(z.form_id) ? x.add(z.form_id) : null
})

因此,如果!x.has(z.form_id)评估为true,则会返回x.add(z.form_id),这也将评估为true - 并且所需对象将添加到已过滤的数组中。

enter image description here

答案 1 :(得分:0)

 var ret = 0
    function filter(filterId) {
      data = formsSummery[0].data_points;
      for (i in data) {
        if (data[i].form_id == filterId) {

          if (ret != 0 && (new Date(data[i].ingest_time).getTime()) >= (new Date(ret.ingest_time).getTime())) {
            ret = (data[i]);
          }
          else {
            ret = (data[i])
          }
        }
      }
      return ret;
    }
    var result = filter(2) //your filter id 
    console.log(result)

不确定日期时间