如何将PI的数字作为字符串并将数字的数组作为数字返回?

时间:2019-02-19 05:50:28

标签: javascript arrays

我需要将pi的前1000个数字作为数组中的字符串,然后将它们作为数字返回到新的数组中:

from this: ["1","4","1","5","9","2"...] (the full array of numbers is already provided in my assignment)
to this: [1, 4, 1, 5, 9, 2...]

我尝试用空数组创建一个新变量,并使用.join方法,但是当我在控制台日志中时,它返回一个空数组。

const strNums = ["1","4","1","5","9","2"...]
const newArray = [];
const integers = strNums.join(newArray);
console.log(newArray);

1 个答案:

答案 0 :(得分:1)

const input = ["1","4","1","5","9","2"];
const output = input.map(Number);