Filtering numbers out from an array

45,893

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);
Share:
45,893
Ambili
Author by

Ambili

Updated on January 29, 2022

Comments

  • Ambili
    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
      Nina Scholz almost 7 years
      you have strings, not numbers in the array.
    • Praveen Kumar Purushothaman
      Praveen Kumar Purushothaman almost 7 years
      digit? you serious? What's res?
    • Weedoze
      Weedoze almost 7 years
      Did you even check on internet how to check if a string is a number ?
    • evolutionxbox
      evolutionxbox almost 7 years
      I would ask that you learn what the basic types of JavaScript are.
    • Nope
      Nope almost 7 years
      Possible duplicate of Check if string contains only digits
    • evolutionxbox
      evolutionxbox almost 7 years
      @Fran surely this question is the negated version of that?
    • evolutionxbox
      evolutionxbox almost 7 years
      @ambili - what is the desired outcome?
    • Nope
      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
    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
    Lajos Arpad almost 7 years
    The question asks us to filter out numbers, so 1.5 should be filtered out as well.
  • Lajos Arpad
    Lajos Arpad almost 7 years
    You are right, parseInt(1.5) returns 1, I thought it returns NaN. My bad
  • Praveen Kumar Purushothaman
    Praveen Kumar Purushothaman almost 7 years
    I really don't understand why people would like to downvote!
  • Lajos Arpad
    Lajos Arpad almost 7 years
    I 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
    Praveen Kumar Purushothaman almost 7 years
    @LajosArpad Agreed. Just curious to know why.
  • gujci
    gujci about 5 years
    I was just about to write the same. this should be the top answer.
  • adiga
    adiga almost 5 years
    This will exclude "0" because 0 is a falsy value
  • аlex dykyі
    аlex dykyі almost 4 years
    filter(Number) will except zeros, you should use filter(Number.isInteger)
  • 20B2
    20B2 almost 4 years
    @аlexdykyі filter(Number.isInteger) will exclude all the integer values like "1", "3",etc in array as well.
  • Nivethan
    Nivethan over 2 years
    this answer worked for me!