【发布时间】:2015-01-09 15:56:40
【问题描述】:
我从 php 脚本返回了以下数组。
$data = [
{
"id": "1",
"created_at": "2015-01-02 15:17:29",
"updated_at": "2015-01-02 15:17:29",
"name": "Minecraft",
"description": "",
"location": "ideaLab",
"cost": "20.00",
"min_age": "7",
"max_age": null,
"start_date": "2014-11-06",
"end_date": "2014-12-11",
"start_time": "16:30:00",
"end_time": "17:30:00",
"registration_start_date": "2014-10-28",
"registration_end_date": "0000-00-00",
"program_id": "1",
"max_attendees": "12"
},
{
"id": "2",
"created_at": "2015-01-02 15:17:29",
"updated_at": "2015-01-02 15:17:29",
"name": "Mini Makers",
"description": "",
"location": "ideaLab",
"cost": "18.00",
"min_age": "9",
"max_age": "7",
"start_date": "2014-11-04",
"end_date": "2014-12-09",
"start_time": "16:30:00",
"end_time": "17:30:00",
"registration_start_date": "2014-10-28",
"registration_end_date": "0000-00-00",
"program_id": "2",
"max_attendees": "20"
}
];
如何通过id选择对象?如果有意义的话,类似于$obj = select * where id=$id 的 SQL 等价物。
【问题讨论】:
-
遍历数组?顺便说一句,只要它是一个字符串,它就只是 JSON。解析后它只是一个 JavaScript 对象。
-
只需将它们全部放入一个列表中遍历它们,直到找到它或使用地图。
-
您可以使用JSONPath 作为更优雅的解决方案
-
任何答案对你有用吗?
标签: javascript jquery