-5

I have an array of strings.

a = ['1', 'car', '2'];

I want to parse only the parsable elements, my objective is:

a = [1, 'car', 2];

How can I do that? What does it happen when I try to parse a[1] ?

Jack
  • 743
  • 2
  • 10
  • 23
  • 1
    use `isNaN`. that function checks if the input is not a number – Mouser Jan 31 '15 at 11:40
  • 3
    `What does it happen when I try to parse ...` - You could have tried that yourself – thefourtheye Jan 31 '15 at 11:40
  • really? that's a pretty useful answer – Jack Jan 31 '15 at 11:49
  • Like @Mouser suggests: use a loop and isNan – qwerty_so Jan 31 '15 at 12:04
  • Before you accept an answer, why don't you check it and make sure it actually does what you are asking. The one you accepted does not. It does not even create an array. The object it does create does not contain the non-numeric values. –  Jan 31 '15 at 12:13

4 Answers4

2

Try

a.map(function(x) { return +x || x; })

+x will try to convert the value to a number, and return NaN if it fails. That is falsy, and cause the RHS of the || to be evaluated, returning the original value.

However, this will fail on '0', so you need to do

a.map(function(x) { return isNaN(x) ? x : +x; }

It's a bit more compact in ES6 environments:

a.map(x => isNaN(x) ? x : +x);
0

Since the purpose of the question has become more clear now, an edit:

     var a = ["1", 'car', 2];
     a = a.map(function(element) {
       //check if element is a number and not NaN or empty string
       if (!isNaN(element) && element != "") {
          return parseInt(element, 10);
       }
       else
       {
          return element;
       }
     });

     document.body.textContent = a;

This will traverse your array. Testing if the array element is a number, if so transform the number if possible. I'm using Array.prototype.map for this. Please note that this only filters integers. If you want to filter floats use parseFloat. That will parse integers and floats.

Mouser
  • 13,132
  • 3
  • 28
  • 54
  • You don't need `isNan(parseInt(element))`; `isNaN(element)` will work fine byitself. Anyway, he doesn't want to filter the array, he wants to convert to numbers where possible. –  Jan 31 '15 at 11:51
  • @torazaburo, still this now map the integers, converting strings to integers where possible and skipping values that are not – Mouser Jan 31 '15 at 11:59
  • Must be missing something. Your solution doesn't transform or replace anything, it just counts the numbers in the array. If that's all you wanted to do, it could be just `a.length - a.filter(isNaN).length`. –  Jan 31 '15 at 12:01
  • @torazaburo It isn't entirely clear what the OP wants parsed. Should the array contain only numbers or should all items that can be converted to a number be converted and the rest left untouched? – Mouser Jan 31 '15 at 12:07
  • You don't need `isNaN(parseInt(...`, just `isNan(element)` will do fine. –  Jan 31 '15 at 13:59
  • 1
    Be careful with this method and empty strings. `!isNaN("")` resolves to true, which is unexpected – camiblanch Sep 05 '18 at 17:14
  • @camiblanch, thank you, updated this to reflect your comment. – Mouser Sep 07 '18 at 11:02
-1

For this you have to loop for each value and check the parse int or not. Here is sample example and create another array.

var count_a = {};
for(var i=0; i<a.length;i++)
    { 
        if (!isNaN(a[i]))
        { 
           count_a[i] = parseInt(a[i], 10);
        }
        else { count_a[i] = a[i] }
    }
    a = count_a

or as first link below you can try this. whenever you want to use of array element.

var a = ['1','2','3'];
var result = a.map(function (x) { 
    return parseInt(x, 10); 
});

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

Parsing every element of an array as an integer

Community
  • 1
  • 1
Ajay2707
  • 5,690
  • 6
  • 40
  • 58
-2

Array construction:

a[0] = 1;
a[1] = 'car';
a[2] = 2;

You need an indexed array.

winston86
  • 159
  • 1
  • 8