如何将一个字符串数组转换为一个数字数组?

时间:2019-09-20 14:11:29

标签: angular typescript turfjs

我有一个字符串数组,如下所示:

Iterator<FacesMessage> pkit = fctxt.getMessages(); // check all existing messages from the FacesContext         
while(pkit.hasNext()){ 
    System.out.println(pkit.next().getDetail()); // get message text
    System.out.println(pkit.next().getSummary());
}

值是字符串。我想将每个值转换为数字。 有人可以解释一下如何做到无循环吗?

问候,

1 个答案:

答案 0 :(得分:1)

数组中的数字不是字符串。但是,如果您有字符串,则可以使用map()Number()将它们转换为数字。

var data = [
  [
    ["5.379856", "43.252967"],
    ["5.422988", "43.249466"],
    ["5.425048", "43.245153"],
    ["5.383804", "43.239838"],
    ["5.399856", "43.212967"],
    ["5.379856", "43.252967"]
  ],
  [
    ["5.39014", "43.279295"],
    ["5.393069", "43.279249"],
    ["5.391814", "43.278421"],
    ["5.390709", "43.278749"],
    ["5.3909", "43.2785"],
    ["5.39014", "43.279295"]
  ]
];

data = data.map(arr => arr.map(item => item.map(value => Number(value))));

console.log(data);