[英]Javascript, using the reduce array method
所以我正在做奧丁項目。 其中一個練習現在暗示使用 reduce 方法。 我有點卡在這里,我已經看過解決方案了。 但是,如果我自己這樣做,我真的不知道自己會犯什么樣的錯誤。 我得到的代碼是這樣的:
let findTheOldest = function(people) {
const oldestPerson = people.reduce((winner, person) => {
if ((person.yearOfDeath - person.yearOfBirth) > (winner.yearOfDeath - winner.yearOfBirth)) {
return person;
} else if ((person.yearOfDeath - person.yearOfBirth) <= (winner.yearOfDeath - winner.yearOfBirth)) {
return winner;
}
});
}
他們擁有的代碼是:
const findTheOldest = function(array) {
return array.reduce((oldest, currentPerson) => {
const oldestAge = getAge(oldest.yearOfBirth, oldest.yearOfDeath)
const currentAge = getAge(currentPerson.yearOfBirth, currentPerson.yearOfDeath)
return oldestAge < currentAge ? currentPerson : oldest
})
}
const getAge = function(birth, death) {
if (!death) {
death = new Date().getFullYear();
}
return death - birth;
}
現在,我知道,如果我看一下,他們的代碼會更加結構化。 但我自己的“解決方案”是我最接近的一個。 到現在為止,我正試圖弄清楚有什么區別。 我知道他們的代碼最終會更好。 它更加整潔,並且使用單獨的變量來存儲年齡更加清晰和清晰。 然而,我和他們都以同樣的方式返回“對象”。 對於我應該能夠通過的第一個測試用例,這就是我現在的想法。
也許我必須解釋測試用例,第一個是根據 yearBirth 和 yearDeath 計算年齡來找到最年長的人。 然后第二個測試用例應該解釋一個還活着的人。 第三個測試用例是活着的人實際上是最年長的人,所以它應該返回那個人。 我現在只嘗試第一個。
我得到的錯誤是“無法讀取未定義的屬性'名稱'”。 我認為這與解決方案試圖訪問我要返回的 object 的 name 屬性有關。 因為這是他們的提示:
有可能沒有yearOfDeath
(如果該人沒有死)在這種情況下,您的代碼將檢索undefined
和undefined - yearOfBirth
會給您NaN
與NaN
的任何比較都是false
的,因此您不會在if
分支中使用 go ,在else if
分支中也不會。 因此,您不會返回任何東西。 如果你用一個簡單的else
替換else if
你會返回一些東西,但它可能是一個錯誤的結果,因為比較是錯誤的。
這就是為什么如果沒有yearOfDeath
,他們制作了 function 來檢索當前年份。
您還需要在 function 的末尾返回oldestPerson
變量。
在您的代碼中,您可以添加類似(person.yearOfDeath || currentYear)
:
let findTheOldest = function(people) { const currentYear = new Date().getFullYear(); const oldestPerson = people.reduce((winner, person) => { if (((person.yearOfDeath || currentYear) - person.yearOfBirth) > ((winner.yearOfDeath || currentYear) - winner.yearOfBirth)) { return person; } else if (((person.yearOfDeath || currentYear) - person.yearOfBirth) <= ((winner.yearOfDeath || currentYear) - winner.yearOfBirth)) { return winner; } }); return oldestPerson; } const p1 = { yearOfBirth: 1990, yearOfDeath: 2015 } const p2 = { yearOfBirth: 1990 } // Edge case: calculate his age with current year console.log(findTheOldest([p1, p2]))
現在這段代碼很難閱讀,這就是為什么最好將它拆分成單獨的函數。
我注意到的幾件事——
const
(而不是let
)reduce
數據集為空時防止程序炸毀reduce
中使用復合結果可以防止我們在每次迭代中重新計算oldest
的人的年齡NaN
。 默認值還向程序員表明 function 接受什么類型的數據 const findTheOldest = (people = []) => people.reduce ( ([ oldest, oldestAge ], person) => { const age = getAge(person) // only compute age of current person return age > oldestAge? [ person, age ]: [ oldest, oldestAge ] }, [ undefined, -Infinity ] // initial value ) [0] // reduce returns [ <person>, <age> ], [0] gets the person out const getAge = ({ yearOfBirth = 0, yearOfDeath = 0 }) => yearOfDeath? yearOfDeath - yearOfBirth: (new Date).getFullYear() - yearOfBirth const peeps = [ { name: "Alice", yearOfBirth: 2000 }, { name: "Gertrude", yearOfBirth: 1910 }, { name: "Martha", yearOfBirth: 1900, yearOfDeath: 2006 }, { name: "Wanda", yearOfBirth: 1940 } ] // works as expected console.log(findTheOldest(peeps)) // { name: "Gertrude", ... } // works when data source is empty console.log(findTheOldest([])) // undefined // works when data is missing. console.log(findTheOldest()) // undefined
試試這個:
let findTheOldest = function(people) { const oldestPerson = people.reduce((winner, person) => { if ((person.yearOfDeath - person.yearOfBirth) > (winner.yearOfDeath - winner.yearOfBirth)) { return person; } else if ((person.yearOfDeath - person.yearOfBirth) <= (winner.yearOfDeath - winner.yearOfBirth)) { return winner; } }); return oldestPerson; }
是一樣的,但是我只是在 function 的末尾添加了 return oldPerson。 告訴我它是否有效
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.