Home > Software engineering >  How to remove NaN from a string in an array
How to remove NaN from a string in an array

Time:01-21

I have this array that is used by Svg to create a map. It contains one big string. The problem is that there are NaNs in the array and it is not able to read the array properly. How can I remove these NaNs?

Array [
  "M214.00002913287273,NaNL214.0000224099021,NaNL214.00002913287273,NaNL214.00002913287273,NaNL214.00011653149096,NaNL214.00011317000562,NaNL214.00011317000562,NaNL214.00000784346574,214.00018930549527L214.0000224099021,NaNL213.999936131779,213.99969560711412L213.999936131779,213.99969560711412L214.00011317000562,NaNL214.00002913287273 ...
]

CodePudding user response:

array = ["M214.00002913287273,NaNL214.0000224099021,NaNL214.00002913287273,NaNL214.00002913287273,NaNL214.00011653149096,NaNL214.00011317000562,NaNL214.00011317000562,NaNL214.00000784346574,214.00018930549527L214.0000224099021,NaNL213.999936131779,213.99969560711412L213.999936131779,213.99969560711412L214.00011317000562,NaNL214.00002913287273"]

you can map this array and use replace for each string

array = array.map(x => x.replace(/NaN/g,''))

 array = ["M214.00002913287273,NaNL214.0000224099021,NaNL214.00002913287273,NaNL214.00002913287273,NaNL214.00011653149096,NaNL214.00011317000562,NaNL214.00011317000562,NaNL214.00000784346574,214.00018930549527L214.0000224099021,NaNL213.999936131779,213.99969560711412L213.999936131779,213.99969560711412L214.00011317000562,NaNL214.00002913287273"]
array = array.map(x => x.replace(/NaN/g,''))
document.documentElement.innerHTML = array

CodePudding user response:

you can map over the array & replace the cprrupted strings like this

const corruptedStrings = [
  "M214.00002913287273,NaNL214.0000224099021,NaNL214.00002913287273..."
];

const replaceWord = (originalWord, wordToMatch, newValue) => (
  originalWord.replace(new RegExp(wordToMatch, 'g'), newValue);
)

const cleanedStrings = corruptedStrings.map(str => replaceWord(str, 'NaN', ''));
  •  Tags:  
  • Related