Filtering numbers out from an array
Solution 1
I think this is the most precise way to filter out numbers from an array.
str.filter(Number);
If the array contains a number in the form of string, then the resulting array will have the number in the form of string. In your case, the resulting array will be ["1", "2"].
If the original array contains 0 or "0", then they will not be present in the resulting array.
If resulting array should include only integer numbers,
str.filter(Number.isInteger)
This will exclude the number in the form of string like "1", "2", etc.
For both integer and float numbers,
str.filter(Number.isFinite)
Solution 2
Making a small change to your code to make it work, this might possibly work.
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);
Or if you want the numbers:
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);
Solution 3
Use 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);
Solution 4
var str = ["https://xx.jpg","https://xx.jpg","1","https://guide.jpg","2", "/static.jpg" ]
str.filter(item=>!isNaN(parseInt(item)))
parseInt convert number to integer and other values converted to "NaN", isNaN function validate value is either "NaN" or not
https://www.w3schools.com/jsref/jsref_isnan.asp https://www.w3schools.com/jsref/jsref_parseint.asp
Solution 5
const intArray = [];
const strArray = [];
const rest_test_parameters = (...args) => {
args.filter((item) => {
if (parseInt(item)) {
return intArray.push(parseInt(item));
}
strArray.push(item);
});
};
const objects = {
a: "a",
b: "c"
};
rest_test_parameters(1, 2, "99","hello", objects);
console.log("intArray", intArray);
console.log("strArray",strArray);
Ambili
Updated on January 29, 2022Comments
-
Ambili over 2 years
I have an array like below and need to filter out the numbers from it ex:
[1,2]
var str = [ "https://xx.jpg", "https://xx.jpg", "1", "https://guide.jpg", "2", "/static.jpg" ]
I have the below code :
var filtered = str.filter(function(item) { return (typeof item === "number") });
but it is not filtering as it is a string.
How to do it?
-
Nina Scholz almost 7 yearsyou have strings, not numbers in the array.
-
Praveen Kumar Purushothaman almost 7 years
digit
? you serious? What'sres
? -
Weedoze almost 7 yearsDid you even check on internet how to check if a string is a number ?
-
evolutionxbox almost 7 yearsI would ask that you learn what the basic types of JavaScript are.
-
Nope almost 7 yearsPossible duplicate of Check if string contains only digits
-
evolutionxbox almost 7 years@Fran surely this question is the negated version of that?
-
evolutionxbox almost 7 years@ambili - what is the desired outcome?
-
Nope almost 7 years@evolutionxbox - The linked answer checks for digits, is that not what would allow OP to determine which ones are numbers? See other answers using that exact expression only negating it. Negating or not is up to OP, still same expression as in linked answer I think, no?
-
-
Lajos Arpad almost 7 years"1" and "2" should be filtered out instead of the other items, you need isNaN instead of !isNaN. Also, your answer does not address the case of "" or null.
-
Lajos Arpad almost 7 yearsThe question asks us to filter out numbers, so 1.5 should be filtered out as well.
-
Lajos Arpad almost 7 yearsYou are right, parseInt(1.5) returns 1, I thought it returns NaN. My bad
-
Praveen Kumar Purushothaman almost 7 yearsI really don't understand why people would like to downvote!
-
Lajos Arpad almost 7 yearsI think you should not care. I upvoted your answer when I was convinced it is correct. My answer was not upvoted, nor downvoted. I am regularly downvoted as well when I am convinced I have given a correct answer, but this will not stop me from helping.
-
Praveen Kumar Purushothaman almost 7 years@LajosArpad Agreed. Just curious to know why.
-
gujci about 5 yearsI was just about to write the same. this should be the top answer.
-
adiga almost 5 yearsThis will exclude
"0"
because 0 is a falsy value -
аlex dykyі almost 4 yearsfilter(Number) will except zeros, you should use filter(Number.isInteger)
-
20B2 almost 4 years@аlexdykyі filter(Number.isInteger) will exclude all the integer values like "1", "3",etc in array as well.
-
Nivethan over 2 yearsthis answer worked for me!