要遍历数组中的对象的Javascript?

时间:2019-02-20 00:38:44

标签: javascript iteration

我正在尝试使用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": ""
            }
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

在这里,我们使用简单映射函数来获取所需的数组。

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