1

I have a string that contains a mix of characters and numbers. I want to separate out the numbers from the string and store them in an Array. So far I have been able to separate the strings and numbers and put them in Array and then able to get only numbers but the string type data is NaN. I am unable to get rid of NaN values from the Array.

So far my code looks like this:

arrayList = "abcd_456_yasdb_382_h_83_2";
new_arrayList = arrayList.split('_');
console.log(new_arrayList); // Output : [ 'abcd', '456', 'yasdb', '382', 'h', '83', '2' ]


new_arrayList = new_arrayList.map(Number);
console.log(new_arrayList); // Output :  [ NaN, 456, NaN, 382, NaN, 83, 2 ]

onlyNumArray = []

for(i =0; i<new_arrayList.length; i++) {
    // console.log(new_arrayList[i]);
    if(typeof (new_arrayList[i]) === "number" && typeof(new_arrayList[i]) != NaN) {
        onlyNumArray.push(new_arrayList[i])
    }
    else {
        console.log("Not a number")
    }
}
console.log(onlyNumArray)  // Output : [ NaN, 456, NaN, 382, NaN, 83, 2 ]

Output = [ NaN, 456, NaN, 382, NaN, 83, 2 ]
Expected Output = [456, 382,83,2]
1
  • typeof NaN returns 'number', so you'll want to replace that condition with !isNaN(new_arrayList[i]) Commented Jan 26, 2022 at 14:52

4 Answers 4

4

Is there a reason you can't use Array#filter? Also, you can't check for NaN by equality (except that NaN != NaN as Passerby points out). You have to use isNaN (or Number#isNaN).

const input = 'abcd_456_yasdb_382_h_83_2';

const tokens = input
  .split('_')
  .map(Number)
  .filter(i => !isNaN(i));

console.log(tokens);

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

2 Comments

Beat me for a minute :) Technically you can check NaN by equality: .filter((i)=>{return i==i;}) can do the trick too.
True, I hadn't considered checking for the absence of self-equality. Not sure I'd immediately pick up on what was being checked if I came across it in someone else's code.
1

You could use the array.filter() method and do something like this :

const unfilteredArray = [ NaN, 456, NaN, 382, NaN, 83, 2 ]
const numbersArray = unfilteredArray.filter(Number)
// Output : [ 456, 382, 83, 2 ]

1 Comment

.filter(Number) could be problematic when 0 is involved: [83,2,0].filter(Number) -> [83,2]
0
const newArrayList = arrayList.split('_').filter(element => {
  return Number.parseInt(element);
}).map(element => {
  return Number.parseInt(element)
});

1 Comment

While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please include an explanation for your code, as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
0
input = 'abcd_456_yasdb_382_h_83_2';
array = [...input.matchAll(/\d+/g)].map(a => parseInt(a[0]));

console.log(array); // [456, 382, 83, 2]

Explanation:

  1. matchAll returns an array of all the regex matches. in our case, it matches sequence of number(s) and return them in an array

  2. The array returned contains extra elements in it. The first element is the actual matched string. So we must run a .map against our returned array, and convert the first value (our number string) into an int with parseInt

1 Comment

While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please include an explanation for your code, as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

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.