Bradley
Bradley

Reputation: 55

how to convert string values to numbers in a mixed array

I have a mixed array of values all string values. I want to take the string values representing a number and convert them to ints. Here is my array:

const data = 'new york;10.99;2000';

I split it transforming it:

const transData = data.split(';');

transData = ["new york", "10.99", "2000"]

I would like to iterate over that and return two clean int's with a string value for all non number strings. I am a bit stumped. I know this is easy to some but I have tried forEach, for, map, and I still get a NaN value for the first array member. I can't seem to filter or skip it even with an "if" check using for instance:

  for(let i=0; i < transData.length; i++)
    if(transData[i] != NaN){
     transData[i] = + transData[i];
    }else{continue};

I know how this works transData[i] = + transData[i]; I just can't seem to automate this thing with iteration/filter/map/whatever..

Any help greatly appreciated. These are baby steps into big boy/girl thinking in javascript.

Here are some of the methods I have tried:

const data = 'new york;10.99;2000';
const transData = data.split(';');

// transData[1] = + transData[1];
// transData[2] = + transData[2];
console.log(transData);



const filteredTransData = transData.filter(data => data > 0);
filteredTransData.forEach((data, idx) => {
filteredTransData[idx] = + filteredTransData[idx];

Upvotes: 2

Views: 1559

Answers (4)

Raximjon Komiljonov
Raximjon Komiljonov

Reputation: 76

Yes, Maheer Ali's method is good. I just modified all code)

const data = 'new york;10.99;2000;0;-1;-2.45';

let transData = Array
    .from(data.split(';'), x => parseFloat(x))
    .filter( value => !Number.isNaN(value) );

console.log(transData);

Array.from() - creates a number array from the string array.

Array.filter() - removes NaN from the number array

Upvotes: 1

Mohammad Usman
Mohammad Usman

Reputation: 39322

You can simply use || operater with .map() to get the desired output. As NaN is a falsey value so it would return the string under consideration as it is if it can't be converted to a number by Number:

const data = 'new york;10.99;2000';

const result = data.split(";").map(s => Number(s) || s);

console.log(result);

As pointed by @Maheer Ali:

Above solution has a limitation. It won't convert String "0" to Number 0 as 0 is a falsey value.

So we may use some other solution posted or we may explicitly apply a check for zero (0) where this modified array is being used.

Upvotes: 3

Jaydip Jadhav
Jaydip Jadhav

Reputation: 12309

Try something like this using MAP function

data.split(';').map((x)=>{
 x= !isNaN(x)? parseFloat(x):x; 
 return x;
})

Upvotes: 1

Maheer Ali
Maheer Ali

Reputation: 36564

NaN === NaN will always return false. To check if element is NaN you need to use isNaN function.

const transData = ["new york", "10.99", "2000"];
for(let i = 0; i< transData.length; i++){
  if(!isNaN(transData[i])){
    transData[i] = +transData[i];
  }
}
console.log(transData)

Whenever you need to get a new value for each element of array based on previous value its better to use map()

const transData = ["new york", "10.99", "2000"];
const res = transData.map(x => isNaN(x) ? x : +x);
console.log(res)

Upvotes: 2

Related Questions