从数组中过滤数字

时间:2017-06-19 11:08:04

标签: javascript arrays string filter

我有一个如下所示的数组,需要从中搜出数字:[1,2]

var str = [
  "https://xx.jpg",
  "https://xx.jpg",
  "1",
  "https://guide.jpg",
  "2", 
  "/static.jpg"
]

我有以下代码:

var filtered = str.filter(function(item) {
  return (typeof item === "number")
});

但它没有过滤,因为它是一个字符串。

怎么做?

10 个答案:

答案 0 :(得分:3)

对代码进行一些小改动以使其正常工作,这可能会有效。



var str = ["https://xx.jpg", "https://xx.jpg", "1", "https://guide.jpg", "2", "/static.jpg"];
var filtered = str.filter(function (item) {
  return !(parseInt(item) == item);
});
console.log(filtered);




或者如果你想要数字:



var str = ["https://xx.jpg", "https://xx.jpg", "1", "https://guide.jpg", "2", "/static.jpg"];
var filtered = str.filter(function (item) {
  return (parseInt(item) == item);
});
console.log(filtered);




答案 1 :(得分:2)

我认为这是从数组中过滤出数字的最精确方法。

str.filter(Number);

如果数组包含字符串形式的数字,则结果数组将具有字符串形式的数字。在您的情况下,结果数组将为[“ 1”,“ 2”]。

如果原始数组包含0或“ 0”,则它们将不出现在结果数组中。

答案 2 :(得分:1)

如果字符串只包含数字,则可以使用测试字符串的正则表达式。



var array = ["https://xx.jpg", "https://xx.jpg", "1", "https://guide.jpg", "2", "/static.jpg"];

array = array.filter(function (a) {
    return !/^\d+$/.test(a);
});

console.log(array);




答案 3 :(得分:1)

使用isNaN()



var str=["https://xx.jpg","https://xx.jpg","1","https://guide.jpg","2","/static.jpg"];

var filtered = str.filter(function(item) {
     
     return (!isNaN(item)); 
     });
     
console.log(filtered);




答案 4 :(得分:1)

如果要检查字符串是否只包含数字,可以使用regular expressions



var str = ["https://xx.jpg", "https://xx.jpg", "1", "https://guide.jpg", "2", "/static.jpg"];
var filtered = str.filter(function (item) {
  return item.match(/^-?\d+$/);
});
console.log(filtered);




答案 5 :(得分:0)

你在过滤器中使用func helper

 function isNumber(n) {  return !isNaN(parseFloat(n)) && isFinite(n);}

答案 6 :(得分:0)

str = str.filter(function(item) {
    return (item !== 0) && ((!item) || (isNaN(item)));
});

操作的右侧调用filter并传递function,如果项目不为0且返回true并且它是假的或不是数字,则返回false;否则返回""。例如,nullstr应该保留在数组中,直到规范为止。通过这种方法,我们得到了所需的数组,并将其分配给R::findAll()变量。

答案 7 :(得分:0)

const filterNumbers = [123456789, 'limit', 'elite', 987654321, 'destruction', 'present'];

const result = filterNumbers.filter(number => parseInt(number) == number);

console.log(result);

下面是类似的代码,它返回数字而不是字符串。 =>只是functionreturn的替代语法(请参见arrow function expressions),但是会产生相同的结果。

答案 8 :(得分:0)

上面的大多数答案都是好的,但是缺少一件事; 过滤出​​数字数组(既不是整数也不是字符串形式的数字)。

我已经添加了代码片段来解决这些小问题。

var str = ["https://xx.jpg", "https://xx.jpg", "1", "https://guide.jpg", "2.4", "/static.jpg","4"];
var filteredNumbers = str.filter(item=> parseFloat(item) == item).map(item=>parseFloat(item));
console.log(filteredNumbers);

答案 9 :(得分:0)

test2 <- test %>% group_by(Zone1_group) %>% summarise(Zone1_variance = SD(Zone1)) 
test3 <- left_join(test, test2, by = "Zone1_group")
test3 %>% mutate(Zone1_new = if_else(Zone1_variance == 0, NA_real_, Zone1))

  X1.9 ID Zone1 Zone2 Zone3 Zone1_group Zone2_group Zone3_group Zone1_variance Zone1_new
1    1  A     1     1     1           1           1           1      0.0000000        NA
2    2  B     1     2     1           1           1           1      0.0000000        NA
3    3  C     1     1     1           1           1           1      0.0000000        NA
4    4  D     2     2     2           2           2           2      0.5773503         2
5    5  E     3     2     2           2           2           2      0.5773503         3
6    6  F     2     2     2           2           2           2      0.5773503         2
7    7  G     5     4     3           3           3           3      1.0000000         5
8    8  H     6     8     3           3           3           3      1.0000000         6
9    9  I     4     6     3           3           3           3      1.0000000         4

parseInt将数字转换为整数,并将其他值转换为“ NaN”,isNaN函数验证值是否为“ NaN”

https://www.w3schools.com/jsref/jsref_isnan.asp https://www.w3schools.com/jsref/jsref_parseint.asp

相关问题