简体   繁体   English

以数组为参数调用 function

[英]Calling a function with array as an argument

pls I want this function to loop through any array and output a new array base on the logic written but am only getting the initial array when I call the function with the array as argument, pls I need help.请我希望这个 function 循环遍历任何数组和 output 一个新的数组基于编写的逻辑,但是当我调用 ZC1C425268E68385D1AB54Z 时我只得到初始数组。 Below is the code下面是代码

const scores = [35, 68, 70, 38];
const scores1 = [89, 42, 33];
function gradingStudents(...grades) {
  const newScores = grades.map(function (grade) {
    if (grade + 2 >= 40 && grade % 5 >= 3) {
      return grade % 5 == 3 ? grade + 2 : grade + 1;
    } else if (grade + 2 >= 40 && grade % 5 < 3) {
      return grade;
    } else {
      return grade;
    }
  });
  return newScores;
}
console.log(gradingStudents(scores1));

Rest Paramanter syntax (...) is used when you are handling with dynamic number of arguments. Rest 参数语法(...)用于处理 arguments 的动态编号。 Here you are passing only one argument to the function.在这里,您只将一个参数传递给 function。 Remove the ... from the function param and your function will work as expected.从 function 参数中删除... ,您的 function 将按预期工作。

 const scores = [35, 68, 70, 38]; const scores1 = [89, 42, 33]; function gradingStudents(grades) { const newScores = grades.map(function (grade) { if (grade + 2 >= 40 && grade % 5 >= 3) { return grade % 5 == 3? grade + 2: grade + 1; } else if (grade + 2 >= 40 && grade % 5 < 3) { return grade; } else { return grade; } }); return newScores; } console.log(gradingStudents(scores1));

If you are handling the function using Rest Paramanter syntax as you did now, the parameter in the function written as ...grades will combine all the paramaters in the function to a single array. If you are handling the function using Rest Paramanter syntax as you did now, the parameter in the function written as ...grades will combine all the paramaters in the function to a single array. Which means your paramater scores1 will be there inside the grades array as in the below console.这意味着您的参数scores1将在grades数组中,如下面的控制台中所示。

 const scores1 = [89, 42, 33]; function gradingStudents(...grades) { // grades will be an array with scores1 as a sigle element in 0th index console.log(grades) } gradingStudents(scores1);

Looping through grades in the above condition, the each node will be an array which will not satisfy any of the if statement, hence it will return the array itself.在上述条件下循环遍历grades ,每个节点将是一个不满足任何 if 语句的数组,因此它将返回数组本身。 Thats why you are getting the output as an array with the parementer as the single node.这就是为什么您将 output 作为一个数组,将 parementer 作为单个节点。

When declaring your function with rest parameters , you're expected to give your data as multiple arguments that will be automatically wrapped in an array当使用rest 参数声明您的 function 时,您应该将数据作为多个 arguments 自动包装在一个数组中

When giving it an array, you'l get a nested array当给它一个数组时,你会得到一个嵌套数组

 function restParamFunc(...params) { console.log(params) } restParamFunc([1, 2])

You should either transform the rest parameters to a classic parameter您应该将 rest 参数转换为经典参数

 function restParamFunc(params) { console.log(params) } restParamFunc([1, 2])

or use array destructuring when calling the function或在调用 function 时使用数组解构

 function restParamFunc(...params) { console.log(params) } restParamFunc(...[1, 2])

since you are returning grade if the first condition didn't meet.因为如果第一个条件不满足,您将返回成绩 The rest operator is not needed.不需要 rest 运算符。 You can write it as follow:你可以这样写:

function gradingStudents(grades) {
        const newScores = grades.map(function (grade) {
            if (grade + 2 >= 40 && grade % 5 >= 3) {
                return grade % 5 == 3 ? grade + 2 : grade + 1;
            }else {
                return grade;
            }
       });
       return newScores;
  }

You can use forEach also您也可以使用forEach

 const scores = [35, 68, 70, 38]; const scores1 = [89, 42, 33]; function gradingStudents(grades) { let newScore = []; grades.forEach(function (grade) { if (grade + 2 >= 40 && grade % 5 >= 3) { grade = grade % 5 == 3? grade + 2: grade + 1; newScore.push(grade); } else if (grade + 2 >= 40 && grade % 5 < 3) { newScore.push(grade); } else { newScore.push(grade); } }); return newScore; } console.log(gradingStudents(scores)); console.log(gradingStudents(scores1));

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

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