ac3*_*360 0 javascript jquery json
我的JSON在下面.它包含两个对象,每个对象都有几个键值对.如何搜索整个JSON数组并将包含特定字符串的对象作为值?
在这种情况下,我需要使用coupon_code:COUPON1拉取对象,以便我可以拉出该优惠券的ID.
简而言之,我只需要使用coupon_code:COUPON1获取优惠券的ID
[Object, Object]
0: Object
coupon_code: "COUPON1"
created_at: "2013-06-04T13:50:20Z"
deal_program_id: 1
id: 7
updated_at: "2013-06-04T13:50:20Z"
__proto__: Object
1: Object
coupon_code: "COUPON3"
created_at: "2013-06-04T15:47:14Z"
deal_program_id: 1
id: 8
updated_at: "2013-06-04T15:47:14Z"
Run Code Online (Sandbox Code Playgroud)
谢谢 :)
你只需循环遍历数组并查看.在JavaScript中有很多方法可以做到这一点.
例如:
var a = /*...your array...*/;
var index = 0;
var found;
var entry;
for (index = 0; index < a.length; ++index) {
entry = a[index];
if (entry.coupon_code == "COUPON1") {
found = entry;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
或者使用ES5的Array#some方法(对于尚未拥有它的浏览器可以"填充",搜索"es5 shim"):
var a = /*...your array...*/;
var found;
a.some(function(entry) {
if (entry.coupon_code == "COUPON1") {
found = entry;
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26219 次 |
| 最近记录: |