0

I am wondering to how to get number from an array. I have tried its give me NaN error

<script type="text/javascript">

$(function(){

var Arr = [ 'h78em', 'w145px', 'w13px' ]

alert(parseInt(Arr[0]))


})
</script>
2
  • 2
    That's because h78em is not a number. Commented Aug 30, 2012 at 8:04
  • 2
    If you just want all the digits in the string you could use a regex: parseInt(Arr[0].replace(/\D/g), ''). Do you care about more complex cases like abc123def456? Commented Aug 30, 2012 at 8:10

7 Answers 7

3

try with

+Arr[0].replace(/\D/g, '');

Example fiddle: http://jsfiddle.net/t6yCV/

Starting + is working like parseInt() and it is necessary if you need to perform some mathematical operation with the number obtained: in fact

typeof Arr[0].replace(/\D/g,'')  // String
typeof +Arr[0].replace(/\D/g,'') // Number
Sign up to request clarification or add additional context in comments.

Comments

3

Try:

['h78em', 'w145px', 'w13px']
 .map(function(a){return ~~(a.replace(/\D/g,''));});
 //=> [78, 145, 13]

See also

Or use a somewhat more elaborate String prototype extension:

String.prototype.intsFromString = function(combine){
 var nums = this.match(/\d{1,}/g);
 return !nums ? 0 
         : nums.length>1 ? combine ? ~~nums.join('') 
           : nums.map(function(a){return ~~a;}) 
         : ~~nums[0];
};
// usage
'abc23'.intsFromString();          //=> 23
'3abc121cde'.intsFromString();     //=> [3,121]
'3abc121cde'.intsFromString(true); //=> 3121
'abcde'.intsFromString();          //=> 0
// and ofcourse
['h78em', 'w145px', 'w13px'].map(function(a){return a.intsFromString();});
//=> [78, 145, 13]

2 Comments

Nice: just for simplification: [^\d] ==> \D - and unfortunately IE8 doesn't support map
@Fabrizio: yep, adjusted. You can find a Array.prototype.map shim @ developer.mozilla.org/en-US/docs/JavaScript/Reference/…
1

You can build a function that builds the number from your string:

function stringToNum(str){
  num = 0;
  for (i = 0; i < str.length; i++) 
    if (str[i] >= '0' && str[i] <= '9') 
      num = num * 10 + parseInt(str[i]);
  return num;
}

jsFiddle : http://jsfiddle.net/8WwHh/

4 Comments

Wow!, Can you please tell what is the logic behind it
you start with num = 0, and iterate the characters from the string. If you find a digit, you multiply your number with 10 and add the new digit at the end.
What to do with stringToNum('123abc345def')?
It depends: would you like to see the values as separate numbers, or as a single number? The latter case is what your method covers, but for the first case your method wouldn't be sufficient.
1

Try this:

var Arr = [ 'h78em', 'w145px', 'w13px' ]

function stringToNum(str){
  return str.match(/\d+/g);

}

alert(stringToNum(Arr[0]));

http://jsfiddle.net/8WwHh/1/

Comments

0

Yet another quick and dirty solution:

alert(Arr[0].match("\\d+"));

Comments

0

How about

alert(parseInt(Arr[0].replace(/[a-z_A-Z]/g,"")));

jsfiddle

1 Comment

Mind for case sensitiveness. Also, this would make something like "a52eb2" in 522. Not sure if that is correct, you might end up with a different number.
0

Try it,this regex is better

parseInt('h343px'.replace(/^[a-zA-Z]+/,''),10)

1 Comment

Try parseInt('{h343px}'.replace(/^[a-zA-Z]+/,''),10)?

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.