[英]Javascript to iterate through objects in array?
我正在嘗試使用 javascript 遍歷大量金融機構列表(從 8974 個對象壓縮到下面僅 2 個對象)。
我想在新列表中保存“id”和“name”。 我是這方面的初學者,在 w3 網站中嘗試了一些代碼后,無法遍歷捕獲多個“id”和“name”的不同對象。
任何人都知道如何完成迭代並捕獲 json 數組中每個 json 對象中的每個“id”和“name”?
{
"found": 8974,
"displaying": 8974,
"moreAvailable": false,
"createdDate": 1550566839,
"institutions": [
{
"id": 5,
"name": "Chase",
"accountTypeDescription": "Banking",
"phone": "1-800-242-7324",
"urlHomeApp": "https://www.chase.com/",
"urlLogonApp": "https://chaseonline.chase.com/chaseonline/logon/sso_logon.jsp",
"oauthEnabled": false,
"urlForgotPassword": "",
"urlOnlineRegistration": "",
"institutionClass": "banking",
"tpCurrencyCode": "USD",
"specialText": "Please enter your Chase User ID and Password. ",
"emailAddress": "https://www.bankone.com/contactus/#personal",
"address": {
"addressLine1": "270 Park Avenue",
"addressLine2": "270 Park Avenue, New York",
"city": "New York",
"country": "USA",
"postalCode": "10017",
"state": "NY"
}
},
{
"id": 170703,
"name": "WWW Bank",
"accountTypeDescription": "TestFI",
"phone": "21210",
"urlHomeApp": "http://www.finbank.com",
"urlLogonApp": "http://www.finbank.com",
"oauthEnabled": false,
"urlForgotPassword": "",
"urlOnlineRegistration": "",
"institutionClass": "testfi",
"tpCurrencyCode": "USD",
"specialText": "Please enter your WWW Bank User and Password required for login.",
"emailAddress": "finbank@finicity.com",
"address": {
"addressLine1": "Utah",
"addressLine2": "Utah",
"city": "Utah",
"country": "USA",
"postalCode": "",
"state": ""
}
}
]
}
這里我們使用一個簡單的 map 函數來獲取所需的數組。
let data = { "found": 8974, "displaying": 8974, "moreAvailable": false, "createdDate": 1550566839, "institutions": [ { "id": 5, "name": "Chase", "accountTypeDescription": "Banking", "phone": "1-800-242-7324", "urlHomeApp": "https://www.chase.com/", "urlLogonApp": "https://chaseonline.chase.com/chaseonline/logon/sso_logon.jsp", "oauthEnabled": false, "urlForgotPassword": "", "urlOnlineRegistration": "", "institutionClass": "banking", "tpCurrencyCode": "USD", "specialText": "Please enter your Chase User ID and Password. ", "emailAddress": "https://www.bankone.com/contactus/#personal", "address": { "addressLine1": "270 Park Avenue", "addressLine2": "270 Park Avenue, New York", "city": "New York", "country": "USA", "postalCode": "10017", "state": "NY" } }, { "id": 170703, "name": "WWW Bank", "accountTypeDescription": "TestFI", "phone": "21210", "urlHomeApp": "http://www.finbank.com", "urlLogonApp": "http://www.finbank.com", "oauthEnabled": false, "urlForgotPassword": "", "urlOnlineRegistration": "", "institutionClass": "testfi", "tpCurrencyCode": "USD", "specialText": "Please enter your WWW Bank User and Password required for login.", "emailAddress": "finbank@finicity.com", "address": { "addressLine1": "Utah", "addressLine2": "Utah", "city": "Utah", "country": "USA", "postalCode": "", "state": "" } } ] }; let newArr = data.institutions.map(i => { return {id: i.id, name: i.name } }); console.log(newArr);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.