我有一个项目数据,其中包含项目和相关员工的列表。每个员工对象都具有salary
属性。我想做的就是找到薪水最高的员工。
代码示例:
var projects = [
//Object(0)
{
projectName: "Winter",
projectCode: "O0123",
employee: [{
title: "Mr.",
name: "Tom",
id: 1005,
salary: 12345
},
{
title: "Mr.",
name: "Bunny",
id: 1009,
salary: 54321
},
{
title: "Mr.",
name: "Harris",
id: 1010,
salary: 23456
},
]
},
//Object(1)
{
projectName: "Summer",
projectCode: "P10406",
employee: [{
title: "Mr.",
name: "Seth",
id: 1006,
salary: 1234
},
{
title: "Mr.",
name: "Sam",
id: 1011,
salary: 654321
},
],
}
]
console.log(projects.length);
let maxSalary = 0;
for (var i = 0; i < projects.length; i++) {
console.log(projects[i].projectName);
for (var j = 0; j < projects[i].employee.length; j++) {
console.log("\t" + projects[i].employee[j].title + projects[i].employee[j].name + "\n" + "\t" + "Salary: " + projects[i].employee[j].salary);
if (i == 0 && j == 0) {
maxSalary <= projects[i].employee[j].salary;
}
if (projects[i].employee[j].salary > maxSalary) {
maxSalary = projects[i].employee[j].salary;
}
}
}
console.log("Max Salary = " + maxSalary);
请提出任何建议。
答案 0 :(得分:2)
只需循环遍历不同的projects
,然后循环employees
即可获得最大值。
var projects = [{
projectName: "Winter",
projectCode: "O0123",
employee: [
{title: "Mr.", name: "Tom", id: 1005, salary: 12345},
{title: "Mr.", name: "Bunny", id: 1009, salary: 54321},
{title: "Mr.", name: "Harris", id: 1010, salary: 23456}
]
},
{
projectName: "Summer",
projectCode: "P10406",
employee: [
{title: "Mr.", name: "Seth", id: 1006, salary: 1234},
{title: "Mr.", name: "Sam", id: 1011, salary: 654321}
]
}
];
var max = 0;
projects.forEach(p => p.employee.forEach(e => e.salary > max && (max = e.salary)));
console.log(max);
如果您想收到问题中提到的employee
,而不是薪水,则可以基本相同,只需退还全部object
:
var projects = [{
projectName: "Winter",
projectCode: "O0123",
employee: [
{title: "Mr.", name: "Tom", id: 1005, salary: 12345},
{title: "Mr.", name: "Bunny", id: 1009, salary: 54321},
{title: "Mr.", name: "Harris", id: 1010, salary: 23456}
]
},
{
projectName: "Summer",
projectCode: "P10406",
employee: [
{title: "Mr.", name: "Seth", id: 1006, salary: 1234},
{title: "Mr.", name: "Sam", id: 1011, salary: 654321}
]
}
];
var max = {salary: 0};
projects.forEach(p => p.employee.forEach(e => e.salary > max.salary && (max = e)));
console.log(max);
我认为这些示例将为您提供基本的方法。
答案 1 :(得分:0)
在这里,您必须混合使用flatMap,这会将您的数组数组转换为简单的平面数组。
然后您可以使用array reduce领取最高薪水。
const projects = [
{
employee: [{
salary: 12345
},
{
salary: 54321
},
{
salary: 23456
},
]
},
{
employee: [{
salary: 1234
},
{
salary: 654321
},
]
}
];
const salaries = projects.flatMap(project => {
// here we have array of array, goal is to craft flat array of salary.
return project.employee.map(employe => {
// From each employee, we pickup only the salary.
return employe.salary;
});
});
const highest = salaries.reduce((accumulator, currentValue) => {
// If current salary is highest than the previous, we keep track of it.
if(currentValue > accumulator) {
accumulator = currentValue;
}
return accumulator;
});
答案 2 :(得分:0)
您已经有了最高薪水,您需要做的就是尽快获得薪水最高的员工的索引。您的代码应如下所示。
var projects = [
//Object(0)
{
projectName: "Winter",
projectCode: "O0123",
employee: [{
title: "Mr.",
name: "Tom",
id: 1005,
salary: 12345
},
{
title: "Mr.",
name: "Bunny",
id: 1009,
salary: 54321
},
{
title: "Mr.",
name: "Harris",
id: 1010,
salary: 23456
},
]
},
//Object(1)
{
projectName: "Summer",
projectCode: "P10406",
employee: [{
title: "Mr.",
name: "Seth",
id: 1006,
salary: 1234
},
{
title: "Mr.",
name: "Sam",
id: 1011,
salary: 654321
},
],
}
]
console.log(projects.length);
let maxSalary = 0;
let employeeWithMaxSalary = {};
for (var i = 0; i < projects.length; i++) {
console.log(projects[i].projectName);
for (var j = 0; j < projects[i].employee.length; j++) {
console.log("\t" + projects[i].employee[j].title + projects[i].employee[j].name + "\n" + "\t" + "Salary: " + projects[i].employee[j].salary);
if (i == 0 && j == 0) {
maxSalary <= projects[i].employee[j].salary;
}
if (projects[i].employee[j].salary > maxSalary) {
maxSalary = projects[i].employee[j].salary;
employeeWithMaxSalary = projects[i].employee[j];
}
}
}
console.log("Max Salary = " + maxSalary);
console.log(employeeWithMaxSalary);
通过使用ES6语法(箭头功能和foreach
)来代替嵌套循环,还可以改善代码。