javascript - 如何在返回之前格式化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"}
]
}
]
解决方案
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])
}
}
推荐阅读
- spring - spring 中的 cors 配置有什么区别:`CorsWebFilte`r vs `WebFluxConfigurerComposite.addCorsMappings(CorsRegistry registry)`?
- javascript - Shopware 5.5 前端模板更改不会加载
- c# - 为什么更改对象比例时不单击统一
- google-chrome - 如何从 Visual Studio 代码启动 Chrome?
- c++ - 如何使用 Opencv 按列对矩阵进行子采样
- c# - 检查元素是否在占位符c#下
- node.js - 猫鼬条件必填字段验证
- html - 在 navbar 下做一个 Div 填满所有空间
- javascript - 为什么 Vue-Router 不能与“.”一起使用 象征?
- c++ - 为什么编译器要向 lib 添加内联类方法?