简体   繁体   English

数据为空时,Java代码会中断

[英]Javascript code breaks when data has null

I am using the following code which works great but totally stops working when "thsub" is null and does not continue reading the rest of the data and just returns a TypeError saying "thsub in null" 我正在使用以下代码,该代码效果很好,但当“ thsub”为null时完全停止工作,并且不会继续读取其余数据,而只是返回一个TypeError,说“ thsub in null”

Here is the code: 这是代码:

var data = {
  "cars": [{
      "id": "1",
      "name": "name 1",
      "thsub": [{
        "id": "11",
        "name": "sub 1",
        "stats": {
          "items": 5,
        },
        "ions": null
      }, {
        "id": "22",
        "name": "sub 2",
        "stats": {
          "items": 5,
        },
        "translations": null
      }],
      "image": null
    },

    {
      "id": "2",
      "name": "name 2",
      "thsub": null, //this will break the code
      "image": null
    }
  ]
}



var thCount = [];

for (var l = 0, m = data.cars.length; l < m; l++) {
  thCount[l] = 0;
  for (var i = 0, j = data.cars[l].thsub.length; i < j; i++) {
    if (data.cars[l].thsub[i].stats) {
      thCount[l]+=data.cars[l].thsub[i].stats.items;
    }
  }
}

console.log(thCount);

How can I fix this? 我怎样才能解决这个问题?

It breaks because you're referencing thsub 's length property. 由于您引用thsublength属性而thsub中断。 As null has no properties, and cannot, this will throw an error. 由于null没有属性,也没有属性,因此将引发错误。 This can, in this situation and every other situation where you're using null, easily be worked around by adding some sort of conditional that will either break your loop or avoid executing the code. 在这种情况下以及您使用null的所有其他情况下,可以通过添加某种条件来轻松解决此问题,这些条件会破坏您的循环或避免执行代码。

Here's a simple example: 这是一个简单的例子:

for (var l = 0, m = data.cars.length; l < m; l++) {
  if (data.cars[l].thsub) {
    for (var i = 0, j = data.cars[l].thsub.length; i < j; i++) {
      if (data.cars[l].thsub[i].stats) {
        thCount[l]+=data.cars[l].thsub[i].stats.items;
      }
    }
  }
}

Note the added data.cars[l].thsub -- if this value is null, the condition will evaluate to false, and the exception-causing code will never execute. 请注意添加的data.cars[l].thsub -如果此值为null,则条件将评估为false,并且永远不会执行导致异常的代码。

Just add a guard condition before you access a property as, an error will be thrown if you try to invoke properties on null or undefined objects. 只需在访问属性之前添加一个保护条件,否则,如果尝试在nullundefined对象上调用属性,则会引发错误。

for (var l = 0, m = data.cars.length; l < m; l++) {
  thCount[l] = 0;
  let tsubs = data.cars[l].thsub || []; <--- Guard condition
  for (var i = 0, j = tsubs.length; i < j; i++) {
    if (data.cars[l].thsub[i].stats) {
      thCount[l] += data.cars[l].thsub[i].stats.items;
    }
  }
}

In the fixed use case tsubs will be set to an empty array when ever the property access returns a falsy value. 在固定用例中,如果属性访问返回虚假值,则tsubs将被设置为空数组。

Your loop is checking the length attribute of thsub. 您的循环正在检查thsub的length属性。 If thsub is null, then your code is attempting to check an attribute (length) of a null object, which will always break your code.. Try adding a check for the thsub object itself before checking the length: 如果thsub为null,则您的代码正在尝试检查null对象的属性(长度),这将总是破坏您的代码。在尝试检查长度之前,请尝试添加对thsub对象本身的检查:

for (var l = 0, m = data.cars.length; l < m; l++) {
  thCount[l] = 0;
  if (data.cars[l].thsub) {
    for (var i = 0, j = data.cars[l].thsub.length; i < j; i++) {
      if (data.cars[l].thsub[i].stats) {
        thCount[l]+=data.cars[l].thsub[i].stats.items;
      }
    }
  }
}

Working sample: 工作样本:

https://jsfiddle.net/mspinks/bwmwntay/5/ https://jsfiddle.net/mspinks/bwmwntay/5/

Just check the thsub variable first: 只需先检查thsub变量:

var thsub;

for (var l = 0, m = data.cars.length; l < m; l++) {
  thCount[l] = 0;
  thsub = data.cars[l].thsub;

  if (!(thsub instanceof Array)) continue; // If it's not an array, skip it.

  for (var i = 0, j = thsub.length; i < j; i++) {
    if (!thsub[i].stats) continue; // If it doesn't contain 'stats', skip it.

    thCount[l] += thsub[i].stats.items;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM