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

时间:2019-03-25 04:18:48

标签: javascript node.js

我有json数据,我是从API中获得的,该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}
        ]
    }
]

我希望能够对phones数组中的每个手机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"}
            ]
        }
    ]

1 个答案:

答案 0 :(得分:3)

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])
    }
}