【问题标题】:count occurrence word in array javascript计算数组javascript中出现的单词
【发布时间】:2015-12-30 11:25:29
【问题描述】:

我有数据结果:

{
  "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');
  }

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

【问题讨论】:

  • 看起来this.odp.length > 0false
  • 你能更新多台设备的 JSON
  • 究竟哪个值为空?您的第一个 console.log 日志记录是否为空?或者是可行性结果数组?
  • @Glubus 'this.odp.length' isundefined...

标签: javascript arrays extjs string-length


【解决方案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"}]}

我的意思是把设备放在刹车 [] 里面,然后你可以调用属性长度。 否则你必须改变条件,例如

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

希望有帮助

【讨论】:

  • 响应来自另一个程序,我刚刚从中获取数据。我的意思是,我不能改变反应。无论如何要让它发生?谢谢,我会添加条件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 2013-02-01
  • 2019-07-31
相关资源
最近更新 更多