Intl.NumberFormat 返回 NaN

问题描述 投票:0回答:1

我正在尝试使用 Intl.NumberFormat 将数组对象中的数字转换为货币,但 NaN 不断出现。

如果可行的话,我该如何将值传递给适合该 ID 的 HTML 元素?

 array1 =  [
  {
    "ID": "1",
    "type": "minivan",
    "registration": new Date('2017-01-03'),
    "price": 7532
  },
  {
    "ID": "2",
    "type": "station wagon",
    "registration": new Date('2018-03-03'),
    "price": 55235
  }]

const options = { style: "currency", currency: "GBP" };
const numberFormat = new Intl.NumberFormat("en-GB", options);

console.log(numberFormat.format(`$array1.price`));

javascript dom currency
1个回答
0
投票

array1
是一个数组。您必须使用索引或循环遍历项目。

 array1 =  [
  {
    "ID": "1",
    "type": "minivan",
    "registration": new Date('2017-01-03'),
    "price": 7532
  },
  {
    "ID": "2",
    "type": "station wagon",
    "registration": new Date('2018-03-03'),
    "price": 55235
  }]

const options = { style: "currency", currency: "GBP" };
const numberFormat = new Intl.NumberFormat("en-GB", options);

array1.forEach(item => {
  const price = numberFormat.format(item.price);
  console.log(`${price}`);
});

© www.soinside.com 2019 - 2024. All rights reserved.