2

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];

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Brilliant. I new it had to be way easier than I was making it. Thanks so much Mohammad!
@Bradley This will not convert '0' to number. Because Number('0') will return 0 and its a falsy value
Maheer you are correct sir. It appeared to work for me as I did not have a 0. Thanks for pointing that out to me!
2

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)

1 Comment

That is how my mind wants to see the solution, using a loop. Thanks Maheer!
1

Try something like this using MAP function

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

1 Comment

I like this one. I am learning a lot about map here. Thanks Jaydip!
1

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

1 Comment

Nice. Clearly explained. "Array.from() - creates a number array from the string array. Array.filter() - removes NaN from the number array"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.