简体   繁体   English

为什么我使用 javascript 的 reduce function 返回“undefined”

[英]Why does my use of javascript's reduce function return "undefined"

1) I've got an array of objects, called employees. 1)我有一组对象,称为员工。

2) I'm trying to find the highest paid employee in the array. 2)我试图在数组中找到收入最高的员工。

3) I'm using Javascript's reduce function (inside function findHighestPaid) to do so. 3)我正在使用 Javascript 的 reduce function(在 function findHighestPaid 内)来这样做。

4) Running findHighestPaid(employees) returns "undefined" when I expected it to return the object that has property name: Abelard. 4)运行 findHighestPaid(employees) 返回“undefined”,而我预计它会返回属性名称为 Abelard 的 object。

Here's my code.这是我的代码。

const employees = [
  {
    name: "Aziz",
    salary: 17050,
  },
  {
    name: "Angela",
    salary: 17060,
  },
  {
    name: "Abelard",
    salary: 17070,
  },
];

function findHighestPaid(arrayOfObjects) {
  let myObject = {
    name: "pretendEmployee",
    salary: 17040,
  };

  arrayOfObjects.reduce(myFunction, myObject);

  function myFunction(acc, item) {
    let personSalary = item.salary;
    let accSalary = acc.salary;
    if (Math.sign(personSalary - accSalary) > 0) {
      console.log(`returning object for ${item.name}`);
      return item;
    } else {
      console.log(`returning object for ${acc.name}`);
      return acc;
    }
  } // end myFunction
}

When I run findHighestPaid(employees) and look in console I see:当我运行 findHighestPaid(employees) 并查看控制台时,我看到:

returning object for Aziz // (as I expected)为 Aziz 返回 object //(如我所料)

returning object for Angela // (as I expected)为 Angela 返回 object //(如我所料)

returning object for Abelard // (as I expected)为 Abelard 返回 object //(如我所料)

undefined // ( oh oh!!! )未定义//(哦哦!!!

Why does the reduce function return "undefined"?为什么 reduce function 返回“undefined”?

the undefined that's being printed at the end is the return value of findHighestPaid() function that you run.最后打印的undefined是您运行的findHighestPaid() function 的返回值。

since you're not returning anything it's returning undefined .因为你没有返回任何东西,所以它返回undefined if your goal is to see the return value of reduce() add a return statement to the function like this:如果您的目标是查看reduce()的返回值,请将 return 语句添加到 function,如下所示:

 const employees = [ { name: "Aziz", salary: 17050, }, { name: "Angela", salary: 17060, }, { name: "Abelard", salary: 17070, }, ]; function findHighestPaid(arrayOfObjects) { let myObject = { name: "pretendEmployee", salary: 17040, }; return arrayOfObjects.reduce(myFunction, myObject); function myFunction(acc, item) { let personSalary = item.salary; let accSalary = acc.salary; if (Math.sign(personSalary - accSalary) > 0) { console.log(`returning object for ${item.name}`); return item; } else { console.log(`returning object for ${acc.name}`); return acc; } } // end myFunction } console.log(findHighestPaid(employees));

Your code is working perfectly although you don't need Math.sign at all, just return the result:尽管您根本不需要Math.sign ,但您的代码运行良好,只需return结果:

 const employees = [ { name: "Aziz", salary: 17050, }, { name: "Angela", salary: 17080, }, { name: "Abelard", salary: 17070, }, ]; function findHighestPaid(arrayOfObjects) { let myObject = { name: "pretendEmployee", salary: 17040, }; return arrayOfObjects.reduce(myFunction, myObject); function myFunction(acc, item) { let personSalary = item.salary; let accSalary = acc.salary; if (personSalary - accSalary > 0) { console.log(`returning object for ${item.name}`); return item; } else { console.log(`returning object for ${acc.name}`); return acc; } } // end myFunction } console.log('Highest paid', findHighestPaid(employees));

you just have to put it like this你只需要这样说

function findHighestPaid(arrayOfObjects) {
    return arrayOfObjects.reduce((prev, curr) => {
        if(!prev) {
            return curr;
        }

        return curr.salary > prev.salary ? curr : prev;    
    }, null);
}

const highestPaidEmployee = findHighestPaid(employees);

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

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