我正在尝试在一系列对象中找到最年长的人。在下面的示例中,我的最终答案应为“ Carly”,因为她已经存活了最长的时间。
我的代码返回了Ray,因为它无法识别Carly仍然活着。
我尝试将代码嵌套在if / else语句中,该语句检查是否存在yearOfDeath值,但显然有问题,因为它仍在返回Ray对象。
let findTheOldest = function(arr) {
var oldest = 0;
var oldestPerson = {};
var today = new Date();
var yyyy = today.getFullYear();
arr.reduce((total, person) => {
if (person.yearOfDeath){
if ((person.yearOfDeath - person.yearOfBirth) > oldest){
oldest = person.yearOfDeath - person.yearOfBirth;
oldestPerson = person;
}
}
else {
person.yearOfDeath = yyyy;
if ((person.yearOfDeath - person.yearOfBirth) > oldest){
oldest = person.yearOfDeath - person.yearOfBirth;
oldestPerson = person;
}
}
});
console.table(oldestPerson);
console.table(arr);
}
const people = [
{
name: 'Carly',
yearOfBirth: 1066,
},
{
name: 'Ray',
yearOfBirth: 1962,
yearOfDeath: 2011
},
{
name: 'Jane',
yearOfBirth: 1912,
yearOfDeath: 1941
},
];
findTheOldest(people);
需要任何帮助吗?
答案 0 :(得分:1)
我这样做是这样的:
const people = [
{
name: 'Carly',
yearOfBirth: 1066,
},
{
name: 'Ray',
yearOfBirth: 1962,
yearOfDeath: 2011
},
{
name: 'Jane',
yearOfBirth: 1912,
yearOfDeath: 1941
},
];
function findOldest(arr){
let ages = people.map(el => {
let age = el.yearOfDeath == undefined ? new Date().getFullYear() - el.yearOfBirth : el.yearOfDeath - el.yearOfBirth;
return age;
})
let maxAge = Math.max(...ages);
let index = ages.indexOf(maxAge);
return arr[index];
}
console.log(findOldest(people));
答案 1 :(得分:1)
如果将reduce
替换为for
循环,则可以使用。不知道为什么需要在那里打reduce
。
let findTheOldest = function(arr) {
var oldest = 0;
var oldestPerson = {};
var today = new Date();
var yyyy = today.getFullYear();
for (let i in arr) {
const person = arr[i];
if (person.yearOfDeath){
if ((person.yearOfDeath - person.yearOfBirth) > oldest){
oldest = person.yearOfDeath - person.yearOfBirth;
oldestPerson = person;
}
}
else {
person.yearOfDeath = yyyy;
if ((person.yearOfDeath - person.yearOfBirth) > oldest){
oldest = person.yearOfDeath - person.yearOfBirth;
oldestPerson = person;
}
}
}
return oldestPerson;
}
const people = [
{
name: 'Carly',
yearOfBirth: 1066,
},
{
name: 'Ray',
yearOfBirth: 1962,
yearOfDeath: 2011
},
{
name: 'Jane',
yearOfBirth: 1912,
yearOfDeath: 1941
},
];
const oldest = findTheOldest(people);
console.log(oldest); // shows {name: "Carly", yearOfBirth: 1066}
请注意,您正在修改原始数组元素(设置yearOfDeath
)。如果要避免这种情况,请直接与该人还活着时的整个年度进行比较,而不是先将其设置为该对象。
稍微改进一下代码,这就是我在这里所做的:
if ((person.yearOfDeath || currentYear) - person.yearOfBirth > oldest)
->如果yearOfDeath
是undefined
,将使用当前年份进行比较。
我还应用了reduce
来遍历所有元素,假设您说这实际上是必需的。我在那里使用了带有两个蓄电池的物体。另外,请检查您是否也需要对其进行初始化。
let findTheOldest = function(arr) {
const currentYear = new Date().getFullYear();
const result = arr.reduce(({ oldest, oldestPerson }, person) => {
if ((person.yearOfDeath || currentYear) - person.yearOfBirth > oldest) {
oldest = (person.yearOfDeath || currentYear) - person.yearOfBirth;
oldestPerson = person;
}
return { oldest, oldestPerson };
}, { oldest: 0, oldestPerson: {} });
return result.oldestPerson;
}
const people = [
{
name: 'Carly',
yearOfBirth: 1066,
},
{
name: 'Ray',
yearOfBirth: 1962,
yearOfDeath: 2011
},
{
name: 'Jane',
yearOfBirth: 1912,
yearOfDeath: 1941
},
];
const oldest = findTheOldest(people);
console.log(oldest); // shows {name: "Carly", yearOfBirth: 1066}
答案 2 :(得分:1)
此处最好使用.sort()
而不是.reduce()
。按yearOfDeath - yearOfBirth
降序排序,并返回已排序数组的第一个元素:
let findTheOldest = function(arr) {
var oldest = 0;
var oldestPerson = [];
var today = new Date();
var yyyy = today.getFullYear();
oldestPerson = arr.sort((personA, personB) => {
if (personA.yearOfDeath && personB.yearOfDeath) {
return (personA.yearOfDeath - personA.yearOfBirth) < (personB.yearOfDeath - personB.yearOfBirth) ? 1 : -1;
}
else {
if (!personA.yearOfDeath) personA.yearOfDeath = yyyy;
if (!personB.yearOfDeath) personB.yearOfDeath = yyyy;
return (personA.yearOfDeath - personA.yearOfBirth) < (personB.yearOfDeath - personB.yearOfBirth) ? 1 : -1;
}
})[0];
console.log(oldestPerson); //logs the Carly object
console.log(oldestPerson.name); //logs "Carly"
}