计算数组javascript中的出现单词

时间:2015-12-30 11:25:30

标签: javascript arrays extjs string-length

我有数据结果:

{
  "statusCode": "T",
  "statusMessage": "Success",
  "resultFeasibility": {
    "resultStatus": 7005,
    "resultInfo": "Fiber
     access is available"
  },
  "devices": {
    "deviceID": "2573631",
    "hostName": "ODP-SKB-FCP\/003 FCP\/D01\/003.01",
    "networkLocation": "ODP-SKB-FCP\/003",
    "technology": "GPON",
    "stoCode": "SKB",
    "maxSpeed": null,
    "double": 0,
    "icon": "resources\/images\/icon-gphone-blue.png",
    "address": {
      "latitude": "-6.915",
      "longitude": "106.9185",
      "zipCode": null,
      "district": null,
      "city": null,
      "streetName": null,
      "lineOne": null
    },
    "markerId": "7-2573631",
    "type": "ALPRO"
  }
}

我想显示'deviceID'出现多少,这是我的代码: 代码:

if (result.statusCode == 'T') {
  this.odp = result.devices;
  console.log(this.odp);
  if (this.odp.length > 0) {
    var alproType = this.odp.technology;
    if (alproType == 'DSLAM')
      this.feasibilityResult.push('feasibility DSLAM: ' + this.odp.length + ' DSLA');
    else if (alproType == 'MSAN')
      this.feasibilityResult.push('feasibility MSAN: ' + this.odp.length + ' MSAN');
    else
      this.feasibilityResult.push('feasibility ODP: ' + this.odp.length + ' ODP');
  }

但输出为空。有人可以帮帮我吗?感谢

1 个答案:

答案 0 :(得分:1)

发生的事情是设备不是一个数组,这就是为什么没有长度,你必须将json更改为:

{"statusCode":"T","statusMessage":"Success","resultFeasibility":{"resultStatus":7005,"resultInfo":"Fiber
 access is available"},"devices":[{"deviceID":"2573631","hostName":"ODP-SKB-FCP\/003 FCP\/D01\/003.01"
,"networkLocation":"ODP-SKB-FCP\/003","technology":"GPON","stoCode":"SKB","maxSpeed":null,"double":0
,"icon":"resources\/images\/icon-gphone-blue.png","address":{"latitude":"-6.915","longitude":"106.9185"
,"zipCode":null,"district":null,"city":null,"streetName":null,"lineOne":null},"markerId":"7-2573631"
,"type":"ALPRO"}]}

我的意思是将设备放在brakets []中然后你可以调用属性长度。 否则,您必须更改条件,例如

if(this.odp != undefined){...}

希望这个帮助