构建一个显示来自api数据的应用。
我想将此从api返回的数据population: 2324388更改为数组,以便可以使用.toLocaleString()。
我想当前显示这样的人口2,324,388这样显示的是2324388。

这是我的数据对象:

Object
data:
name: "Namibia"
topLevelDomain: [".na"]
alpha2Code: "NA"
alpha3Code: "NAM"
callingCodes: ["264"]
capital: "Windhoek"
altSpellings: (3) ["NA", "Namibië", "Republic of Namibia"]
region: "Africa"
subregion: "Southern Africa"
population: 2324388
latlng: (2) [-22, 17]
demonym: "Namibian"
area: 825615
gini: 63.9
timezones: ["UTC+01:00"]
borders: (4) ["AGO", "BWA", "ZAF", "ZMB"]
nativeName: "Namibia"
numericCode: "516"
currencies: (2) [{…}, {…}]
languages: (2) [{…}, {…}]
translations: {de: "Namibia", es: "Namibia", fr: "Namibie", ja: "ナミビア", it: "Namibia", …}
flag: "https://restcountries.eu/data/nam.svg"
regionalBlocs: [{…}]
cioc: "NAM"

最佳答案

如果population是字符串或数字,则只需执行以下操作:

var populationString = (data && data.population && data.population.toLocaleString()) || '';

07-24 18:46