简体   繁体   中英

Javascript to iterate through objects in array?

I'm trying to iterate through a large list of financial institutions (condensed from the 8974 objects to only 2 below) using javascript.

I want to save "id" and "name" in a new list. I'm a beginner at this and after trying some code in w3 website was unable to iterate through the different object capturing multiple "id"s and "name"s.

Anyone have an idea how to accomplish iterating through and capturing every "id" and "name" in each json object within the json array?

{
    "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": ""
            }
        }
    ]
}

Here we use a simnple map function to get desired array.

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM