当我使用fetch时,我正在获取json格式的数据。我想通过cust_id搜索json并仅返回匹配的记录。请告诉我该怎么做。我必须出示该人拥有的所有信用卡。
"objects": [
{
"card_number": "123412341234",
"created_at": "2018-10-06T06:02:25.053942",
"cust_id": "12345",
"id": 1,
"resource_uri": "/api/card/1/",
"status": "completed"
},
{
"card_number": "213412345678",
"created_at": "2018-10-06T06:53:22.588967",
"cust_id": "12345",
"id": 3,
"resource_uri": "/api/card/3/",
"status": "pending"
},
]
答案 0 :(得分:0)
我希望对您有帮助
var val={
"objects": [
{
"card_number": "123412341234",
"created_at": "2018-10-06T06:02:25.053942",
"cust_id": "12345",
"id": 1,
"resource_uri": "/api/card/1/",
"status": "completed"
},
{
"card_number": "213412345678",
"created_at": "2018-10-06T06:53:22.588967",
"cust_id": "12345",
"id": 3,
"resource_uri": "/api/card/3/",
"status": "pending"
},
]
};
function get(val,custid){
var result;
for(var i=0;i<val.objects.length;i++){
if(val.objects[i].cust_id=custid){
result= val.objects[i];
}
}
return result;
}
console.log(get(val,"1235"));
对于多条记录,您可以将结果作为数组,并使用array.push插入结果(函数变量)并在末尾返回数组
答案 1 :(得分:0)
var objects= [
{
"card_number": "123412341234",
"created_at": "2018-10-06T06:02:25.053942",
"cust_id": "12345",
"id": 1,
"resource_uri": "/api/card/1/",
"status": "completed"
},
{
"card_number": "213412345678",
"created_at": "2018-10-06T06:53:22.588967",
"cust_id": "12345",
"id": 3,
"resource_uri": "/api/card/3/",
"status": "pending"
},
{
"card_number": "213412345678",
"created_at": "2018-10-06T06:53:22.588967",
"cust_id": "12345",
"id": 3,
"resource_uri": "/api/card/3/",
"status": "pending"
},
]
console.log(objects.filter(i=>i['cust_id'] === '12345'))