如何从json数组

时间:2015-10-09 09:47:11

标签: jquery json

如何使用以下JSON检查或显示call_no IS NOT NULL的最后一个call_no。请帮忙。

  

[      {       “CLIENT_NAME”:空,       “call_no”: “空”     },     {       “CLIENT_NAME”:空,       “call_no”:“null”},{       “client_name”:“Acheiva Technologies”,       “call_no”:1},{       “client_name”:“Allied International”,       “call_no”:2},{“client_name”:“Advantage”,“call_no”:3},{“client_name”:null,“call_no”:“null”}]

2 个答案:

答案 0 :(得分:0)

您可以使用 filter()

var data = [{
  "client_name": null,
  "call_no": "null"
}, {
  "client_name": null,
  "call_no": "null"
}, {
  "client_name": "Acheiva Technologies",
  "call_no": 1
}, {
  "client_name": "Allied International",
  "call_no": 2
}, {
  "client_name": "Advantage",
  "call_no": 3
}, {
  "client_name": null,
  "call_no": "null"
}];

// filtering elements 
var res = data.filter(function(v) {
   // checking `call_no` value
   return v["call_no"] !== "null";
});

// getting last object from filtered arrary
var result = res[res.length - 1];

// getting values from last object
document.write(result["call_no"],'<br>',result["client_name"]);

答案 1 :(得分:0)

&#13;
&#13;
var data = [{
  "client_name": null,
  "call_no": "null"
}, {
  "client_name": null,
  "call_no": "null"
}, {
  "client_name": "Acheiva Technologies",
  "call_no": 1
}, {
  "client_name": "Allied International",
  "call_no": 2
}, {
  "client_name": "Advantage",
  "call_no": 3
}, {
  "client_name": null,
  "call_no": "null"
}];

callNo = data[0].call_no;

for(i = 0; i < data.length; i++){
  if(data[i].call_no != "null"){
    callNo = data[i].call_no;
  } 
}
alert(callNo)
&#13;
&#13;
&#13;