【问题标题】:Javascript to iterate through objects in array?Javascript遍历数组中的对象?
【发布时间】:2019-02-20 00:38:44
【问题描述】:

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

【问题讨论】:

  • after trying some code in w3 - 什么代码?你试过什么?如果您访问帮助中心并阅读how to ask,以及如何创建minimal, complete, and verifiable example,您会在此处获得更好的回复。
  • Stack Overflow 上有很多很多关于如何从 JSON 对象中提取数据的问题。你调查过哪些?如果您认为它们不适用于您的情况,请编辑您的问题并解释原因,以便我们为您提供最好的帮助。

标签: javascript iteration


【解决方案1】:

这里我们使用一个简单的 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    相关资源
    最近更新 更多