﻿ How to Convert All Elements in an Array to Integer in JavaScript - ITCodar

# How to Convert All Elements in an Array to Integer in JavaScript

## How to convert all elements in an array to integer in JavaScript?

You need to loop through and parse/convert the elements in your array, like this:

``var result_string = 'a,b,c,d|1,2,3,4',    result = result_string.split("|"),    alpha = result,    count = result,    count_array = count.split(",");for(var i=0; i<count_array.length;i++) count_array[i] = +count_array[i];//now count_array contains numbers``

You can test it out here. If the `+`, is throwing, think of it as:

``for(var i=0; i<count_array.length;i++) count_array[i] = parseInt(count_array[i], 10);``

## Convert string array to integer array

Use `map()` and `parseInt()`

``var res = ['2', '10', '11'].map(function(v) {  return parseInt(v, 10);});document.write('<pre>' + JSON.stringify(res, null, 3) + '<pre>')``

## convert string arrays to integer then get the sum

You can easily achieve this result first parsing

``"[3, 43, 3, 3, 5]"``

to

``[3, 43, 3, 3, 5]``

using JSON.parse and then use reduce to add the array elements

``const array = [  "[3, 43, 3, 3, 5]",  "[3, 43, 3, 3, 5]",  "[3, 43, 3, 3, 5]",  "[3, 43, 3, 3, 5]",  "[3, 43, 3, 3, 5]",  "[3, 43, 3, 3, 5]",];const result = array.map((a) => JSON.parse(a).reduce((acc, curr) => acc + curr));console.log(result);``
``/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */.as-console-wrapper {  max-height: 100% !important;  top: 0;}``

## Convert integer array to string array in JavaScript

You can use map and pass the String constructor as a function, which will turn each number into a string:

``sphValues.map(String) //=> ['1','2','3','4','5']``

This will not mutate sphValues. It will return a new array.

## How to convert nested arrays with strings to numbers in JavaScript?

Use `Array#map` method with a recursive approach for the nested array.

``function conv(arr) {  // iterate over the array  return arr.map(function(v) {    // if the element is an array call the function recursively    // or parse the number and treat NaN as 0    return Array.isArray(v) ? conv(v) : Number(v) || 0;  })}``

``const array = [  ["4000", "2000", "1000", ""],  ["6000", "7000", "3000"]];const array1 = ["1000", "5000", "3000"];function conv(arr) {  return arr.map(function(v) {    return Array.isArray(v) ? conv(v) : Number(v) || 0;  })}console.log(conv(array));console.log(conv(array1));``

## How to convert a string of numbers to an array of numbers?

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 casex=>+x                             // no multiple args, just 1 function call``

I hope it is a bit more clear.

## convert array of numbers that are strings into array of numbers

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);``

## converting array to different types (lodash/js)

Use `Array#map` with the index of the loop.