0

I have an array var data=["test","10%","abc","20%"];

Now is there any way so that i can filter out and get only the numbers something like result=["10%","20%"];

6
  • 1
    I doubt those are numbers.. Commented Mar 30, 2016 at 10:42
  • there are many ways to achieve such result, but show your code first. Commented Mar 30, 2016 at 10:43
  • 1
    What exactly defines a number you want to keep? Is it digits followed by a % symbol? Have you tried writing a regular expression for that and using it with Array#filter? Commented Mar 30, 2016 at 10:45
  • @RyanO'Hara, Why data.filter(parseInt); fails ? Commented Mar 30, 2016 at 10:51
  • @RayonDabre: That particular expression isn’t a very good idea because Array#filter passes three arguments, the second of which is the index of the element being tested and which will be interpreted by parseInt as a radix. As for the approach of using parseInt in general: I never said it was wrong. It’s hard to know what’s right. Using if (parseInt(e)) will fail if you didn’t want negative numbers, or if you did want zero, or if you didn’t want strings like 0xf, or if you didn’t want strings like 7#?zQl, or if you wanted 0.5%. Commented Mar 30, 2016 at 12:20

4 Answers 4

1

You can use this function:

Explanation: We create a new array and push all the elements of the old array,
if the first letter can be converted into a digit.

var data = ["test", "10%", "abc", "20%"];

function onlyNumbers(array) {
  var newArr = [];
  array.forEach(function(e) {
    if (parseInt(e.charAt(0))) {
      newArr.push(e);
    }
  })
  return newArr;
}

var dataNew = onlyNumbers(data); // <-- ["10%","20%"]

document.write(JSON.stringify(dataNew));

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the solution , you got the question right and got me what i needed
This will include items such as "1xyz%", "2/()abs" and so on... You only check that the first character is a number.
@henrikmerlander That is correct. It is only a solution for this type of scenario. If the question or the situation would be more specific, we would have to modify the code.
Yeah it was just a heads up to the person accepting the answer. Maybe it is not the solution right away but a hint on how to do it. I still think the best solution is to use a regular expression though.
@henrikmerlander yup,I was stuck with this problem,and John gave me an idea how can I achieve it.. As you said using regex will always be better
0

This will work out your problem and create new array with only good elements:

function check_percentage(num){
    var regex = /^(\d+%)$/;
    if (num.match(regex)){
        return true;
    }
}

var result=["babab","fg210%","120%","babab","4214210%","20%"],
    filtered_result=[];

result.forEach(function(k,v){
    if (check_percentage(k)){
        filtered_result.push(k);
    }
});

console.log(filtered_result);

https://jsfiddle.net/LLc1zp9h/

Comments

0

You can use an underscore library and do:

var data=["test","10%","abc","20%"];

var result = _.filter(data, function(item) { 
     return item.match(/^(\d+|\d+[.]\d+)%?$/); 
});

document.write(JSON.stringify(result));
<script src="http://underscorejs.org/underscore-min.js"></script>

And you can do the same without external library:

var data=["test","10%","abc","20%"],
     result = [];

 for(var i = 0; i < data.length; i++) { 
     if (data[i].match(/^(\d+|\d+[.]\d+)%?$/)) { 
          result.push(data[i]); 
     } 
 }

document.write(JSON.stringify(result));

1 Comment

no need to use any libraries, pure JS RegExp object has exec method, which does the same.
0

var data = ["test", "10%", "abc", "20%"];
var result = [];
data.forEach(function(v) {
  if (parseInt(v) > 0) result.push(parseInt(v));
});
console.log(result);

If you need not the numbers but the string values, then just replace: result.push(parseInt(v)); => result.push(v);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.