-2

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": ""
            }
        }
    ]
}
zero298
  • 25,467
  • 10
  • 75
  • 100
  • `after trying some code in w3` - what code? What have you tried? You'll get a much better response here if you visit the help centre and read [how to ask](https://stackoverflow.com/questions/how-to-ask), and how to create a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve). – Don't Panic Feb 20 '19 at 00:43
  • Possible duplicate of [For-each over an array in JavaScript?](https://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript) – Dan O Feb 20 '19 at 00:47
  • 1
    there are many, many questions on Stack Overflow regarding how to extract data from inside a JSON object. Which ones have you investigated? If you don't feel they apply to your situation, please edit your question and explain why so we can best help you. – Dan O Feb 20 '19 at 00:48

1 Answers1

0

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);
Bibberty
  • 4,670
  • 2
  • 8
  • 23