JavaScript数组映射,返回大于20的数字

时间:2018-01-27 10:26:40

标签: javascript arrays

我尝试在parseInt(string,10)之后编写> = 20,但这不起作用。所以在我将数组项转换为整数后,我是否检查它们是否大于20?

         const strings = ["10", "20","40","","50", "60", "70"];
         const numbers = strings.map(string => parseInt(string,10));
         console.log(numbers);

1 个答案:

答案 0 :(得分:0)

您可以将Array#filter与条件一起使用,并映射获取数字数组的值。



var strings = ["10", "20","40","","50", "60", "70"],
    numbers = strings
        .filter(v => +v >= 20)
        .map(Number);

console.log(numbers);