简体   繁体   English

为什么函数返回 undefined 而不是给定的元素?

[英]Why is function returning undefined instead of the given element?

I'm trying to solve the problem below:我正在尝试解决以下问题:

// Given an object and a key, "getNthElementOfProperty" returns the nth element of an array located at the given key.

// SAMPLE DATA:

    var obj = {
      key: [1, 2, 6]
    };
    var output = getNthElementOfProperty(obj, 'key', 1);
    console.log(output); // --> 2

    function getNthElementOfProperty(obj, key, n) {
      if(obj[key] === undefined){
        return undefined
      } else if(Array.isArray(obj[key]) === false){
        return undefined
      // otherwise if array empty
      } else if(obj[key].length === 0){
        return undefined
      }
      for(var e in obj[key]){
        if(e === n){
          return obj[key][e];
        } 
      }
      return undefined;
    }

I have tested and all below conditions have been met:我已经测试过并且满足以下所有条件:

If the array is empty, it should return undefined.
If n is out of range, it should return undefined.
If the property at the given key is not an array, it should return undefined.
If there is no property at the key, it should return undefined.

The function returns undefined instead of the nth element.该函数返回undefined而不是第 n 个元素。 I'm not looking for the answer, but instead why it's returning undefined .我不是在寻找答案,而是为什么它返回undefined

: https://i.stack.imgur.com/Wk1dC.png : https://i.stack.imgur.com/Wk1dC.png

Just remove the loop.只需删除循环。 JavaScript returns undefined if the index is not inside the array.如果索引不在数组内,JavaScript 将返回undefined You can also get rid of if (array.length === 0) because the index can't be inside an array of length 0.你也可以去掉if (array.length === 0)因为索引不能在长度为 0 的数组内。

function getNthElementOfProperty(obj, key, n) {
  if(obj[key] === undefined){
    return undefined
  } else if(Array.isArray(obj[key]) === false){
    return undefined
  }   
  return obj[key][n];
}

If you want to know why your loop didn't work:如果你想知道为什么你的循环不起作用:

for (let i in array) assigns the index as a string to i . for (let i in array)将索引作为字符串分配给i The === operator checks for types and sees that '0' !== 0 for example. ===运算符检查类型并看到例如'0' !== 0

Turns out that instead of a for in loop I needed to use a regular for loop.事实证明,我需要使用常规的 for 循环而不是 for in 循环。

function getNthElementOfProperty(obj, key, n) {
    if(obj[key] === undefined){
      return undefined
    } else if(Array.isArray(obj[key]) === false){
      return undefined
    } else if(obj[key].length === 0){
      return undefined
    }
    for(var i = 0; i < obj[key].length; i++){
        if(i === n){
            return obj[key][i]
        }
    }
    return undefined;
  };

Simple function with all validation no need for loop, you can use hasOwnProperty instead of obj[key] === undefined带有所有验证的简单函数不需要循环,您可以使用hasOwnProperty而不是obj[key] === undefined

function getNthElementOfProperty(obj, key, n) {
    //validate obj and key and n
    if (!obj || !key || isNaN(n)) {
        return undefined;
        // if key in not in Object
    } else if (!obj.hasOwnProperty(key)) {
        return undefined;
        // if property at key is not a array
    } else if (!Array.isArray(obj[key])) {
        return undefined;
        // if array is empty
    } else if (!obj[key].length) {
        return undefined;
        // if n is greater than Array length 
    } else if (obj[key].length < n) {
        return undefined;
    } else {
        return obj[key][n]
    };
};

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

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