简体   繁体   English

在 function 内包含数组元素的 For 循环。 如何退回 function output?

[英]For loop with array elements Inside function. How to return the function output?

How can i get the output of these below two for loop's console.log via function call.我如何通过 function 调用获得以下两个 for 循环的 console.log 的 output 。 Code snippet given below.下面给出的代码片段。

CODE BELOW:下面的代码:

 var scoreDolphins = [96, 108, 89]; var scoreKolas = [88, 91, 110]; var avgD_Div = scoreDolphins.length; console.log(avgD_Div); var avgK_Div = scoreKolas.length; console.log(avgK_Div); var calcAvgF = function() { for (let scoreItem_D = 0; scoreItem_D < scoreDolphins.length; scoreItem_D++) { console.log(scoreDolphins[scoreItem_D]); } console.log("---------------------------------------------------------------------------"); for (let socreItem_K = 0; socreItem_K < scoreKolas.length; socreItem_K++) { console.log(scoreKolas[socreItem_K]); } return calcAvgF; } console.log(calcAvgF);

calcAvgF should not return calcAvgF iteslf. calcAvgF不应返回calcAvgF itemslf。 Also you should call calcAvgF function unstead of console.log(calcAvgF)你也应该调用calcAvgF function 而不是console.log(calcAvgF)

 var scoreDolphins = [96, 108, 89]; var scoreKolas = [88, 91, 110]; var avgD_Div = scoreDolphins.length; var avgK_Div = scoreKolas.length; var calcAvgF = function () { for (let scoreItem_D = 0; scoreItem_D < scoreDolphins.length; scoreItem_D++) { console.log(scoreDolphins[scoreItem_D]); } for (let socreItem_K = 0; socreItem_K < scoreKolas.length; socreItem_K++) { console.log(scoreKolas[socreItem_K]); } } calcAvgF();

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

相关问题 在函数内返回for循环 - Return a for loop inside a function 如何将数组发送到其他函数。(jQuery) - How to send array to other function.(Jquery) 将数组和其他参数传递给函数。 怎么样? - Pass an array and further arguments into a function. How? 如何从使用reduce函数计算得出的数组中返回求和数组。 Vuejs - How to return summed array from computed using reduce function. Vuejs 如何在FUNCTION中返回多变数组的输出 - how to return to be output for mutlidimention array in FUNCTION 如何使用ajax在函数内放置元素数组? - How to place an array of elements inside a function with ajax? 如何为.push函数制作数组。 收到“ typeError:.push不是函数”? - How to make array for a .push function. Getting “typeError: .push is not a function”? 将函数分配给数组的元素。 如何绑定函数的输出? - assigning function to elements of array . How to bind output of function? 如何解析属性onclick,其中包含返回函数。 javascript - how to parse attribute onclick, which contains return function. javascript JavaScript:调用Add函数返回新数组的高阶函数; 错误:输出数组中的&#39;NaN&#39;元素 - JavaScript: Higher Order Function that Calls Add Function to Return New Array; Error: 'NaN' elements in output array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM