简体   繁体   English

在 function 中返回“typeof”值?

[英]Returning a "typeof" value in a function?

I'm a total beginner, so it would be very helpful if someone posted the full solution in the comments.我是一个完全的初学者,所以如果有人在评论中发布完整的解决方案会非常有帮助。

I'm trying to solve the following challenge on edabit:我正在尝试解决 edabit 上的以下挑战:

Create a function that takes an array and returns the types of values (data types) in a new array.创建一个 function,它接受一个数组并在新数组中返回值的类型(数据类型)。

arrayValuesTypes([1, 2, "null", []])
// expected output ➞ ["number", "number", "string", "object"]

arrayValuesTypes(["214", true, false, 2, 2.15, [], null])
// expected output ➞ ["string", "boolean", "boolean", "number", "number", "object", "object"]

arrayValuesTypes([21.1, "float", "array", ["I am array"], null, true, 214])
// expected output ➞ ["number", "string", "string", "object", "object", "boolean", "number"]

So far I have this:到目前为止,我有这个:

    arr = [1, 2, "null", []]
    
    function arrayValuesTypes(arr) {
        for (let i = 0; i < arr.length; i++) {
         return typeof arr[i]
      }
    }

// output ➞
// 'number'

But when I change "return" to a console.log, it gives me a result closer to what I am looking for.但是当我将“返回”更改为 console.log 时,它给了我一个更接近我正在寻找的结果。 Why is that?这是为什么?

arr = [1, 2, "null", []]

function arrayValuesTypes(arr) {
    for (let i = 0; i < arr.length; i++) {
     console.log(typeof arr[i])
  }
}
// output ➞
// 'number'
// 'number'
// 'string'
// 'object'

When you wrote 'return', it will do the loop once and then stop.当你写'return'时,它会循环一次然后停止。 why?为什么? because return stops the action.因为 return 停止动作。 In this case, it stops the loop.在这种情况下,它会停止循环。 And that's why you got the output of the first item in the array- type number这就是为什么您得到数组类型编号中第一项的 output

Why is that?这是为什么?

Because return inside a function will return the value and so stop the function, regardless of the for loop.因为在function内部return将返回该值,因此无论for循环如何,都会停止 function。


So use map() to apply it for each item in the array, and then return that array created by map :所以使用map()将它应用于数组中的每个项目,然后返回由map创建的数组:

 function arrayValuesTypes(arr) { return arr.map(tmp => typeof tmp); } const tests = [ [1, 2, "null", []], ["214", true, false, 2, 2.15, [], null], [21.1, "float", "array", ["I am array"], null, true, 214] ]; for (let testIndex in tests) { console.log(arrayValuesTypes(tests[testIndex])); }

["number", "number", "string", "object"] 
["string", "boolean", "boolean", "number", "number", "object", "object"] 
["number", "string", "string", "object", "object", "boolean", "number"]

This should do the work:这应该做的工作:

let arr = [1, 2, "null", []]


function arrayValuesTypes(arr) {
    return arr.map(a=>typeof a)
}

Try this:尝试这个:

 arr = [1, 2, "null", []] function arrayTypes(arr) { //Create an empty list to hold values arrayWithTypes = [] //Loop for (var x of arr) { //Push values in the above empty list arrayWithTypes.push(String(typeof(x))) } //Return the final array return arrayWithTypes } console.log(arrayTypes(arr))

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

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