首页 > 解决方案 > 如何在返回之前格式化json数组中的所有电话号码

问题描述

我有从 API 获得的 json 数据,该 API 在对象数组中有电话号码。我需要做的是格式化这些 nbrs。

这是我从我调用的 api 获得的数据样本,我想对其进行转换。

[
    {
         "Name": "Tom Miller",
         "emails": [
             {"primary": "email1@test.com"},
             {"secondary": "email2@test.com"}
        ],
         "phones": [
             {"fax": 2015551212},
             {"home": 2134441212},
             {"mobile": 3105551212},
             {"work": 3605551212}
        ]
    },
    {
         "Name": "Bud Light",
         "emails": [
             {"primary": "email1@test.com"},
             {"secondary": "email2@test.com"}
        ],
         "phones": [
             {"fax": 2015551212},
             {"home": 2134441212},
             {"mobile": 3105551212},
             {"work": 3605551212}
        ]
    }
]

我希望能够记录电话数组中的每个电话 nbr 并调用一个函数来格式化 nbr,然后在将 nbr 返回到我的应用程序之前用新格式化的 nbr 替换 nbr。

我调用格式化nbrs的函数是这样的

function formatPhoneNumber(phoneNumberString) {
  var cleaned = ('' + phoneNumberString).replace(/\D/g, '')
  var match = cleaned.match(/^(\d{3})(\d{3})(\d{4})$/)
  if (match) {
    return '(' + match[1] + ') ' + match[2] + '-' + match[3]
  }
  return phoneNumberString
} 

我怎样才能做到这一点 ?

这是我要根据我的功能返回的内容

[

        {
             "Name": "Tom Miller",
             "emails": [
                 {"primary": "email1@test.com"},
                 {"secondary": "email2@test.com"}
            ],
             "phones": [
                 {"fax": "(201) 555-1212"},
                 {"home": "(213) 444-1212"},
                 {"mobile": "(310) 555-1212"},
                 {"work": "(360) 555-1212"}
            ]
        },
        {
             "Name": "Bud Light",
             "emails": [
                 {"primary": "email1@test.com"},
                 {"secondary": "email2@test.com"}
            ],
             "phones": [
                 {"fax": "(201) 555-1212"},
                 {"home": "(213) 444-1212"},
                 {"mobile": "(310) 555-1212"},
                 {"work": "(360) 555-1212"}
            ]
        }
    ]

标签: javascriptnode.js

解决方案


var defaultArray = [
    {
         "Name": "Tom Miller",
         "emails": [
             {"primary": "email1@test.com"},
             {"secondary": "email2@test.com"}
        ],
         "phones": [
             {"fax": 2015551212},
             {"home": 2134441212},
             {"mobile": 3105551212},
             {"work": 3605551212}
        ]
    },
    {
         "Name": "Bud Light",
         "emails": [
             {"primary": "email1@test.com"},
             {"secondary": "email2@test.com"}
        ],
         "phones": [
             {"fax": 2015551212},
             {"home": 2134441212},
             {"mobile": 3105551212},
             {"work": 3605551212}
        ]
    }
]

function formatPhoneNumber(phoneNumberString) {
  var cleaned = ('' + phoneNumberString).replace(/\D/g, '')
  var match = cleaned.match(/^(\d{3})(\d{3})(\d{4})$/)
  if (match) {
    return '(' + match[1] + ') ' + match[2] + '-' + match[3]
  }
  return phoneNumberString
} 

for(let item of defaultArray){
for(let phoneItems in item.phones){
let currentVal = Object.keys(item.phones[phoneItems])[0];
item.phones[phoneItems] = formatPhoneNumber(item.phones[phoneItems][currentVal])
}
}

console.log(defaultArray)

请使用以下代码。让我知道这些是预期的输出。

var defaultArray = [
{
     "Name": "Tom Miller",
     "emails": [
         {"primary": "email1@test.com"},
         {"secondary": "email2@test.com"}
    ],
     "phones": [
         {"fax": 2015551212},
         {"home": 2134441212},
         {"mobile": 3105551212},
         {"work": 3605551212}
    ]
},
{
     "Name": "Bud Light",
     "emails": [
         {"primary": "email1@test.com"},
         {"secondary": "email2@test.com"}
    ],
     "phones": [
         {"fax": 2015551212},
         {"home": 2134441212},
         {"mobile": 3105551212},
         {"work": 3605551212}
    ]
}
]

    function formatPhoneNumber(phoneNumberString) {
  var cleaned = ('' + phoneNumberString).replace(/\D/g, '')
  var match = cleaned.match(/^(\d{3})(\d{3})(\d{4})$/)
  if (match) {
    return '(' + match[1] + ') ' + match[2] + '-' + match[3]
  }
  return phoneNumberString
}



for(let item of defaultArray){
    for(let phoneItems in item.phones){
       let currentVal = Object.keys(item.phones[phoneItems])[0];
           item.phones[phoneItems] = formatPhoneNumber(item.phones[phoneItems][currentVal])
    }
}

推荐阅读