【问题标题】:Javascript loop through JSON objectJavascript 循环遍历 JSON 对象
【发布时间】:2015-09-16 16:16:36
【问题描述】:

我想做的事情超出了我的知识范围。感谢大家的时间和帮助,很高兴得到如此庞大的开发者社区的支持。

问题

我需要遍历一个对象(JSON 响应)以确定哪些数据为真,然后使用结果编辑 html。

json 对象是

  var data = {
  "total": 4,
  "limit": 50,
  "questions": [{
    "date_created": "2015-06-29T18:24:25.000-04:00",
    "item_id": "MLA567045929",
    "seller_id": 186626557,
    "status": "UNANSWERED",
    "text": "Pregunta de Testeo, user 2.",
    "id": 3612747353,
    "deleted_from_listing": false,
    "hold": false,
    "answer": null,
    "from": {
      "id": 186625262,
      "answered_questions": 0
    }
  }, {
    "date_created": "2015-06-29T18:30:16.000-04:00",
    "item_id": "MLA567045929",
    "seller_id": 186626557,
    "status": "UNANSWERED",
    "text": "Lorem ipsum dolor sit amet",
    "id": 3612938882,
    "deleted_from_listing": false,
    "hold": false,
    "answer": null,
    "from": {
      "id": 186625262,
      "answered_questions": 0
    }
  }, {
    "date_created": "2015-06-29T18:30:35.000-04:00",
    "item_id": "MLA567045929",
    "seller_id": 186626557,
    "status": "UNANSWERED",
    "text": "an est odio timeam quaerendum",
    "id": 3612752695,
    "deleted_from_listing": false,
    "hold": false,
    "answer": null,
    "from": {
      "id": 186625262,
      "answered_questions": 0
    }
  }, {
    "date_created": "2015-06-29T18:31:32.000-04:00",
    "item_id": "MLA567045929",
    "seller_id": 186626557,
    "status": "ANSWERED",
    "text": "Responder esta pregunta",
    "id": 3612753455,
    "deleted_from_listing": false,
    "hold": false,
    "answer": {
      "text": "Pregunta respondida",
      "status": "ACTIVE",
      "date_created": "2015-06-29T18:31:58.000-04:00"
    },
    "from": {
      "id": 186625262,
      "answered_questions": 1
    }
  }],
  "filters": {
    "limit": 50,
    "offset": 0,
    "is_admin": false,
    "sorts": [],
    "caller": 186626557,
    "seller": "186626557"
  },
  "available_filters": [{
    "id": "item",
    "name": "Item",
    "type": "text"
  }, {
    "id": "from",
    "name": "From user id",
    "type": "number"
  }, {
    "id": "totalDivisions",
    "name": "total divisions",
    "type": "number"
  }, {
    "id": "division",
    "name": "Division",
    "type": "number"
  }, {
    "id": "status",
    "name": "Status",
    "type": "text",
    "values": ["BANNED", "CLOSED_UNANSWERED", "DELETED", "DISABLED", "UNDER_REVIEW"]
  }],
  "available_sorts": ["item_id", "from_id", "date_created", "seller_id"]
};

我要找的结果是

在数据对象中,我需要提取状态为unansweredquestions 以及与这些未回答问题相关联的id 字段。

 "questions1":[{  "status" : "UNANSWERED",
                "id" : 3612747353}],
"questions2":[{  "status" : "UNANSWERED",
                "id" : 3612938882}],
 ...

根据我的搜索,我尝试了循环、for in 和每个都没有成功。

关于如何达到预期结果的任何建议或想法?我需要将此示例应用于多个对象。

【问题讨论】:

    标签: javascript loops for-loop each


    【解决方案1】:

    尝试一些方便的列表处理函数来在概念上简化它。 filtermap 函数会有所帮助。我提供的过滤功能告诉它只让满足未回答状态的项目通过。我提供的 map 函数将所有从过滤器中出来的对象都变成了它们的 id。

    data["questions"].filter(function(obj) {
        return obj["status"] === "UNANSWERED";
    }).map(function(obj) {
        return obj["id"];
    });
    

    【讨论】:

      【解决方案2】:

      您可以遍历问题并将所需的 id 保存到数组中:

      var questions = data.questions;
      var unanswered = [];
      for(var i = 0, len = questions.length; i < len; i++) {
          if(questions[i].status === 'UNANSWERED') {
              unanswered.push(questions[i].id);
          }
      }
      

      unanswered 将是一组未回答的问题 ID。您不需要保存状态;你知道他们都是“未回答的”。

      【讨论】:

        【解决方案3】:
        var results = [];
        for (var question in data.questions) {
          if (data.questions[question].status === "UNANSWERED") {
            results.push({
              "status" : data.questions[question].status,
              "id" : data.questions[question].id
            });
          }
        }
        
        // Now results contains an array of unanswered questions,
        // with just their status & id.
        

        【讨论】:

        • 对您正在做的事情的一些解释将帮助提出问题的人
        猜你喜欢
        • 2016-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-07
        • 1970-01-01
        • 2014-07-14
        • 2013-03-09
        相关资源
        最近更新 更多