简体   繁体   English

对象属性不存在时返回值

[英]Returning Value When Object Property Doesn't Exist

Running a test on my code. 在我的代码上运行测试。 It should return 0 if the property passed into the function does not exist. 如果传递给函数的属性不存在,则应返回0。 But its not returning anything. 但是它什么也没返回。 Did I make a typo? 我打错字了吗?

var obj = {
  key: [1, 2, 3]
};

function getAverageOfElementsAtProperty(obj, key) {
  if (obj[key].length === 0 || Array.isArray(obj[key]) === false || obj.hasOwnProperty(key) === false) {
    return 0;
  }
  var average = 0;
  for (var i = 0; i < obj[key].length; i++) {
    average += obj[key][i];
  }
  average /= obj[key].length;
  return average;
}

console.log(getAverageOfElementsAtProperty(obj, 'notKey'));

If you don't pass an array to your function, it fails when it tries to get the length of the object that was passed, so that shouldn't be how you test initially. 如果不将数组传递给函数,则当它尝试获取传递的对象的length时,它将失败,因此,这不应该是您最初进行测试的方式。 You just need to see if the property exists and that is done by simply attempting to access the property in an if condition. 您只需要查看该属性是否存在,只需在if条件下尝试访问该属性即可完成。

Now, if you are going to add tests to see if the property does exist and that it is an array, then you have to add more tests to check the items in the array to see if they are numbers, otherwise trying to get a numerical average will fail. 现在,如果要添加测试以查看该属性是否确实存在并且该属性是否为数组,则必须添加更多测试以检查数组中的项是否为数字,否则尝试获取数字平均会失败。

Since there are really two things to do (check if the property is there and get math average), I would break this into two functions: 由于实际上有两件事要做(检查属性是否存在并获得数学平均值),我将其分为两个函数:

 var obj1 = { key: [1, 2, 3] }; var obj2 = { key: [1, "test", 3] }; function getAverageOfElementsAtProperty(obj, key) { if (obj[key] && Array.isArray(obj[key])) { // Ok, we have the right property and there is an array there, // now we have to test that each item in the array is a number var numbers = obj[key].every(function (currentValue) { return typeof currentValue === "number"; }); // Return the average if we have all numbers or 0 if not return numbers ? getAverage(obj[key]) : 0; } else { return 0; } } function getAverage(arr){ var average = 0; // Array.forEach() is much simpler than counting loops arr.forEach(function(item) { average += item; }); return average / arr.length; } console.log(getAverageOfElementsAtProperty(obj1, 'notkey')); // 0 console.log(getAverageOfElementsAtProperty(obj1, 'key')); // 2 console.log(getAverageOfElementsAtProperty(obj2, 'key')); // 0 - obj2 does not contain all numbers 

Since obj['notKey'] does not exist, it does not return an array. 由于obj['notKey']不存在,因此它不返回数组。 Therefore you cannot do .length of undefined. 因此,您不能执行.length的undefined。 I would change it to typeof to see if its defined or not. 我将其更改为typeof以查看其是否已定义。

var obj = {
  key: [1, 2, 3]
};

function getAverageOfElementsAtProperty(obj, key) {

  if (typeof obj[key] == 'undefined' || Array.isArray(obj[key]) === false || obj.hasOwnProperty(key) === false) {
    return 0;
  }

  var average = 0;
  for (var i = 0; i < obj[key].length; i++) {
    average += obj[key][i];
  }
  average /= obj[key].length;
  return average;
}

console.log(getAverageOfElementsAtProperty(obj, 'notKey'));

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

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