[英]How to return year events in a for loop from an array
進階——For 循環
創建一個需要某人年齡的程序,然后輸出他們所經歷的每一年的關鍵事件。
· 擴展,允許用戶輸入他們的生日以提高准確性
· 代碼效率,對關鍵事件使用數組而不是 if 語句。
我創建了由 1993 年的年份和事件組成的數組
那么我如何創建一個 for 循環來獲取用戶的出生年份(輸入)並顯示從那一年到當年的事件?
edYearlyEvents = {
"1993": "Bill Clinton is inaugurated as the 42nd president.",
"1994": "The People's Republic of China gets its first connection to the Internet.",
"1995": "eBay is founded by Pierre Omidyar.",
"1996": "Murder of Tupac Shakur.",
"1997": "The first episode of Pokémon airs on TV Tokyo.",
"1998": "Death of Frank Sinatra.",
"1999": "The Columbine High School massacre in Colorado, United States, causes 15 deaths.",
"2000": "The Sony PlayStation 2 releases in Japan. ",
"2001": " September 11 attacks.",
"2002": "The 2002-2004 SARS outbreak began in Guangdong.",
"2003": "The United States invades Iraq and ousts Saddam Hussein, triggering worldwide protests and an 8 year war",
"2004": "First surface images of Saturn's moon Titan.",
"2005": "Hurricane Katrina kills 1,836 people in the Gulf of Mexico.",
"2006": "Twitter is launched.",
"2007": "Introduction of the iPhone.",
"2008": "Barack Obama is elected to become the first black President of the United States.",
"2009": "The cryptocurrency Bitcoin is launched.",
"2010": "The Burj Khalifa in Dubai becomes the tallest structure in the world, standing at 829.8 m (2,722 ft).",
"2011": "Osama bin Laden is shot dead by United States Navy SEALs in Pakistan.",
"2012": "Vladimir Putin is elected president of Russia for the third time.",
"2013": "Terrorist attacks occur in Boston and Nairobi.",
"2014": "The worst Ebola epidemic in recorded history occurs in West Africa, infecting nearly 30,000 people and resulting in the deaths of 11,000+.",
"2015": "Liquid water is found on Mars.",
"2016": "El Chapo is recaptured following his escape from a high-security prison in Mexico",
"2017": "January 20: Donald Trump is inaugurated as President of the United States.",
"2018": "Saudi Arabia allows women to drive.",
"2019": "A major fire engulfs Notre-Dame Cathedral in Paris, resulting in the roof and main spire collapsing.",
"2020": "The murder of George Floyd sparks protests across the United States and the world.",
"2021": "Supporters of President Donald Trump, gathered after a rally led by him, attack the United States capitol, leading to five deaths.",
"2022": "Monkeypox outbreak",
"2023": "The funeral of Pope Benedict XVI is held at Saint Peter's Square within the Vatican City.",
}
sBirthYear = input("你是哪一年出生的:\n")
對於 dYearlyEvents.items() 中的 sBirthYear:
打印(sBirthYear)
const events = { "1993": "Bill Clinton is inaugurated as the 42nd president.", "1994": "The People's Republic of China gets its first connection to the Inte.net.", "1995": "eBay is founded by Pierre Omidyar.", "1996": "Murder of Tupac Shakur.", "1997": "The first episode of Pokémon airs on TV Tokyo.", "1998": "Death of Frank Sinatra.", "1999": "The Columbine High School massacre in Colorado, United States, causes 15 deaths.", "2000": "The Sony PlayStation 2 releases in Japan. ", "2001": " September 11 attacks.", "2002": "The 2002-2004 SARS outbreak began in Guangdong.", "2003": "The United States invades Iraq and ousts Saddam Hussein, triggering worldwide protests and an 8 year war", "2004": "First surface images of Saturn's moon Titan.", "2005": "Hurricane Katrina kills 1,836 people in the Gulf of Mexico.", "2006": "Twitter is launched.", "2007": "Introduction of the iPhone.", "2008": "Barack Obama is elected to become the first black President of the United States.", "2009": "The cryptocurrency Bitcoin is launched.", "2010": "The Burj Khalifa in Dubai becomes the tallest structure in the world, standing at 829.8 m (2,722 ft).", "2011": "Osama bin Laden is shot dead by United States Navy SEALs in Pakistan.", "2012": "Vladimir Putin is elected president of Russia for the third time.", "2013": "Terrorist attacks occur in Boston and Nairobi.", "2014": "The worst Ebola epidemic in recorded history occurs in West Africa, infecting nearly 30,000 people and resulting in the deaths of 11,000+.", "2015": "Liquid water is found on Mars.", "2016": "El Chapo is recaptured following his escape from a high-security prison in Mexico", "2017": "January 20: Donald Trump is inaugurated as President of the United States.", "2018": "Saudi Arabia allows women to drive.", "2019": "A major fire engulfs Notre-Dame Cathedral in Paris, resulting in the roof and main spire collapsing.", "2020": "The murder of George Floyd sparks protests across the United States and the world.", "2021": "Supporters of President Donald Trump, gathered after a rally led by him, attack the United States capitol, leading to five deaths.", "2022": "Monkeypox outbreak", "2023": "The funeral of Pope Benedict XVI is held at Saint Peter's Square within the Vatican City.", } const yearOfBirth = 1995 const eventsBeforeMyBirth = Object.keys(events).filter(i => Number(i) <= yearOfBirth).map(i => { return { [i]: events[i] } }) console.log(eventsBeforeMyBirth)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.