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>
Jitender
  • 7,593
  • 30
  • 104
  • 210

7 Answers7

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
Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
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]
Community
  • 1
  • 1
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • Nice: just for simplification: `[^\d] ==> \D` - and unfortunately IE8 doesn't support `map` – Fabrizio Calderan Aug 30 '12 at 08:35
  • @Fabrizio: yep, adjusted. You can find a `Array.prototype.map` shim @ https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/map – KooiInc Aug 30 '12 at 08:56
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/

gabitzish
  • 9,535
  • 7
  • 44
  • 65
  • Wow!, Can you please tell what is the logic behind it – Jitender Aug 30 '12 at 08:10
  • 1
    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. – gabitzish Aug 30 '12 at 08:11
  • What to do with `stringToNum('123abc345def')`? – KooiInc Aug 30 '12 at 08:58
  • 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. – KooiInc Aug 30 '12 at 09:14
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/

Dhanasekar Murugesan
  • 3,141
  • 1
  • 18
  • 21
0

How about

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

jsfiddle

Snake Eyes
  • 16,287
  • 34
  • 113
  • 221
  • 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. – Flater Aug 30 '12 at 08:09
0

Yet another quick and dirty solution:

alert(Arr[0].match("\\d+"));
MAK
  • 26,140
  • 11
  • 55
  • 86
0

Try it,this regex is better

parseInt('h343px'.replace(/^[a-zA-Z]+/,''),10)
yao lu
  • 81
  • 4