Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
Replace NaN with "-" character using JavaScript

Can you please tell me how to replace NaN with another character?

There are variables that are assigned some numeric value.

Further, in the function, these variables get their values, which I then display in the html table. But, sometimes some variable (let it be the variable "d") - returns NaN.

To fix this, I separately created an array with variables that already have some values.

Next, I started the iteration loop. If the condition returns NaN, then it should be replaced with "-". But, it doesn't work.

UPD Most likely, I did not correctly make it clear why I created the array. I created an array in order to iterate over all the variables that I have, and if any variable has NaN (null, undefined), then it will be assigned the value "-".

var a, b, c, d, x1, x2, x3, x4;
var elem = document.getElementById.bind(document);

function iFunc () {
  a = 1;
  b = 3;
  c = 2;
  d = NaN;
  
  x1 = elem('a1').innerHTML = a;
  x2 = elem('b1').innerHTML = b;
  x3 = elem('c1').innerHTML = c;
  x4 = elem('d1').innerHTML = d;
  
  var arrX = [x1, x2, x3, x4];
  
  for (var x of arrX) {
    if (x !== x) {
    x = "-"; // the character that was supposed to replace NaN
    console.log(x);
  }
 }
}

iFunc ();
<table>
  <tr>
    <td><span id="a1"></span></td>
    <td><span id="b1"></span></td>
    <td><span id="c1"></span></td>
    <td><span id="d1"></span></td>
  </tr>
 </table>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I'm not 100% sure what you're looking for, but to me it seems you are possibly trying to create a new array with NaN values replaced with "-". If this is the case, you could simply use .map() to iterate, similar to post above 👆

let a;
let b;
let c;
let d;

function iFunc() {
  a = 3;
  b = 4;
  c = 24;
  d = NaN;

  let arrX = [a, b, c, d];

  function mapper(x) {
    if (!isNaN(x)) {
      return x;
    } else {
      return x = "-";
    }
  }
  const arrX2 = arrX.map(x => mapper(x))

  console.log(arrX2);
}
    
iFunc();
about 4 years ago · Juan Pablo Isaza Relatório

0

let a, b, c, d;

  a = 1;
  b = 3;
  c = 2;
  d = NaN;

  
  let arrX = [a, b, c, d];
  for(i in arrX){
   if(isNaN(arrX[i])){
      arrX[i]='-';
   }
  }
console.log(arrX);
about 4 years ago · Juan Pablo Isaza Relatório

0

Use javascript's replace function:

https://www.w3schools.com/jsref/jsref_replace.asp

Note that you will have to coerce those NaN values into proper strings before though.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda