Javascript 遍历 JSON 对象
Javascript loop through JSON object
我想做的事情超出了我的知识范围。感谢大家的时间和帮助,很高兴得到如此庞大的开发者社区的支持。
问题
我需要遍历对象(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"]
};
我要找的结果是:
在数据对象中,我需要提取状态为 unanswered
的 questions
和与这些未回答的问题相关的 id
字段。
"questions1":[{ "status" : "UNANSWERED",
"id" : 3612747353}],
"questions2":[{ "status" : "UNANSWERED",
"id" : 3612938882}],
...
根据我的搜索,我尝试了循环、for in,但都没有成功。
关于如何实现预期结果的任何建议或想法?我需要将此示例应用于多个对象。
尝试一些方便的列表处理函数来从概念上简化它。 filter and map 函数会有所帮助。我提供给过滤器的功能告诉它只允许满足未答复状态条件的项目通过。我提供给 map 的函数将所有从 filter 出来的对象都变成了它们的 id。
data["questions"].filter(function(obj) {
return obj["status"] === "UNANSWERED";
}).map(function(obj) {
return obj["id"];
});
您可以循环遍历问题并将您想要的 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。您不需要保存状态;你知道他们都是 'UNANSWERED'.
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.
我想做的事情超出了我的知识范围。感谢大家的时间和帮助,很高兴得到如此庞大的开发者社区的支持。
问题
我需要遍历对象(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"]
};
我要找的结果是:
在数据对象中,我需要提取状态为 unanswered
的 questions
和与这些未回答的问题相关的 id
字段。
"questions1":[{ "status" : "UNANSWERED",
"id" : 3612747353}],
"questions2":[{ "status" : "UNANSWERED",
"id" : 3612938882}],
...
根据我的搜索,我尝试了循环、for in,但都没有成功。
关于如何实现预期结果的任何建议或想法?我需要将此示例应用于多个对象。
尝试一些方便的列表处理函数来从概念上简化它。 filter and map 函数会有所帮助。我提供给过滤器的功能告诉它只允许满足未答复状态条件的项目通过。我提供给 map 的函数将所有从 filter 出来的对象都变成了它们的 id。
data["questions"].filter(function(obj) {
return obj["status"] === "UNANSWERED";
}).map(function(obj) {
return obj["id"];
});
您可以循环遍历问题并将您想要的 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。您不需要保存状态;你知道他们都是 'UNANSWERED'.
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.