ECMAScript5 provides a map method for Arrays, applying a function to all elements of an array.
Here is an example:
var a = ['1','2','3']
var result = a.map(function (x) {
return parseInt(x, 10);
});
console.log(result);
See Array.prototype.map()
Answer from dheerosaur on Stack OverflowECMAScript5 provides a map method for Arrays, applying a function to all elements of an array.
Here is an example:
var a = ['1','2','3']
var result = a.map(function (x) {
return parseInt(x, 10);
});
console.log(result);
See Array.prototype.map()
You can do
var arrayOfNumbers = arrayOfStrings.map(Number);
- MDN Array.prototype.map
For older browsers which do not support Array.map, you can use Underscore
var arrayOfNumbers = _.map(arrayOfStrings, Number);
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
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]
An easier method is to map to the Number object
result= stringBits.map(Number);
javascript 1.6. has map() ( https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/Map ), so you can do something like
intArray = someArray.map(function(e) { return parseInt(e) })
You can use Array.map to convert each element into a number.
var a = "1,2,3,4";
var b = a.split(',').map(function(item) {
return parseInt(item, 10);
});
Check the Docs
Or more elegantly as pointed out by User: thg435
var b = a.split(',').map(Number);
Where Number() would do the rest:check here
Note: For older browsers that don't support map, you can add an implementation yourself like:
Array.prototype.map = Array.prototype.map || function(_x) {
for(var o=[], i=0; i<this.length; i++) {
o[i] = _x(this[i]);
}
return o;
};
My 2 cents for golfers:
b="1,2,3,4".split`,`.map(x=>+x)
backquote is string litteral so we can omit the parenthesis (because of the nature of split function) but it is equivalent to split(','). The string is now an array, we just have to map each value with a function returning the integer of the string so x=>+x (which is even shorter than the Number function (5 chars instead of 6)) is equivalent to :
function(x){return parseInt(x,10)}// version from techfoobar
(x)=>{return parseInt(x)} // lambda are shorter and parseInt default is 10
(x)=>{return +x} // diff. with parseInt in SO but + is better in this case
x=>+x // no multiple args, just 1 function call
I hope it is a bit more clear.