如何在返回之前格式化 json 数组中的所有电话号码



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

下面是我从调用的 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])
    }
}

最新更新