[英]Why doesn't my arrow function return array but instead a function declaration
I tried to write a arrow function to compute the square of only the positive integers ( not include the fractions). 我试图编写一个箭头函数来计算仅正整数(不包括分数)的平方。
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34]; const squareList = (arr) => { "use strict"; const squaredIntegers = (arr) => { let arrayChoosen = arr.filter(ele => ele > 0 && Number.isInteger(ele)); return arrayChoosen.map(x => x * x); } return squaredIntegers; }; // test your code const squaredIntegers = squareList(realNumberArray); console.log(squaredIntegers);
But the result is a function declaration, not a array as I expect. 但是结果是函数声明,而不是我期望的数组。 But when I try to modify the code in this way
但是当我尝试以这种方式修改代码时
const squareList = (arr) => { "use strict"; let squaredIntegers = arr.filter(ele => ele > 0 && Number.isInteger(ele)); squaredIntegers = squaredIntegers.map(val => val * val); return squaredIntegers; };
Then it output the array I expect. 然后输出我期望的数组。 Why in the first case it doesn't work?
为什么在第一种情况下它不起作用?
You need to return the returned value of your squaredIntegers
function, so try with this code: 您需要返回
squaredIntegers
函数的返回值,因此请尝试使用以下代码:
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34]; const squareList = (arr) => { "use strict"; const squaredIntegers = (a) => { let arrayChoosen = a.filter(ele => ele > 0 && Number.isInteger(ele)); return arrayChoosen.map(x => x * x); } return squaredIntegers(arr); }; // test your code const squaredIntegers = squareList(realNumberArray); console.log(squaredIntegers);
Because you return function name only - that is - reference to function. 因为仅返回函数名称-即-对函数的引用。
Instead, you should call that function: 相反,您应该调用该函数:
return squaredIntegers();
Your second example works fine because you are actually calling arr.filter()
and assign it's value to squaredIntegers
第二个示例工作正常,因为您实际上是在调用
arr.filter()
并将其值分配给squaredIntegers
You need to call squaredIntegers
with arr
for getting a new array. 您需要使用
arr
调用squaredIntegers
以获得新数组。
return squaredIntegers(arr);
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34]; const squareList = (arr) => { "use strict"; const squaredIntegers = (arr) => { let arrayChoosen = arr.filter(ele => ele > 0 && Number.isInteger(ele)); return arrayChoosen.map(x => x * x); } return squaredIntegers(arr); }; // test your code const squaredIntegers = squareList(realNumberArray); console.log(squaredIntegers);
Because the first squareList
returns the function itself, rather than the function called on the input arr
. 因为第一个
squareList
返回函数本身,而不是在输入arr
上调用的函数。 You might return
its invocation instead: 您可以改为
return
其调用:
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34]; const squareList = (arr) => { "use strict"; const squaredIntegers = (arr) => { let arrayChoosen = arr.filter(ele => ele > 0 && Number.isInteger(ele)); return arrayChoosen.map(x => x * x); } return squaredIntegers(arr); }; // test your code const squaredIntegers = squareList(realNumberArray); console.log(squaredIntegers);
Because you return a function when you need to return result of the function call. 因为您在需要返回函数调用的结果时返回了函数。 Check the corrected code.
检查更正的代码。
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34]; const squareList = (arr) => { "use strict"; const squaredIntegers = (arr) => { let arrayChoosen = arr.filter(ele => ele > 0 && Number.isInteger(ele)); return arrayChoosen.map(x => x * x); } return squaredIntegers(arr); }; // test your code const squaredIntegers = squareList(realNumberArray); console.log(squaredIntegers);
You are returning a function rather than a function call. 您返回的是函数而不是函数调用。
Change return squaredIntegers;
更改
return squaredIntegers;
to return squaredIntegers(arr)
; return squaredIntegers(arr)
;
So it will become 所以它将成为
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
"use strict";
const squaredIntegers = (arr) => {
let arrayChoosen = arr.filter(ele => ele > 0 && Number.isInteger(ele));
return arrayChoosen.map(x => x * x);
}
return squaredIntegers(arr);
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.