Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

149
Vistas
getting extra value in JS array

I want to create from user input two arrays while calling a function. The issue is that I get an extra value in the array that I did not type in; it’s looks like a default value...

I have the following code:

const NameArr = [];
const IdArr = [];

function getName() {
  const Input = document.getElementById("input").value;
  const list_names = Input.split(", ");

  for (let i of list_names) {
    NameArr.push(i);
    IdArr.push(i);
  };
  console.log(NameArr, IdArr);
  return NameArr, IdArr;
};

getName();
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <script src="main.js" charset="utf-8" defer></script>
    <title></title>
  </head>
  <body>
    <form id="form" action="#" method="post">
      <input type="text" id="input">
      <button onclick="getName();">Get Data</button>
    </form>
  </body>
</html>

I am not sure why I get an extra value [""] in my arrays...

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You are calling getName() at the end of your code.

Inside getName you are getting the value of your input field which is initially the empty string ""

const Input = document.getElementById("input").value;

Then you are using the push() method which will push the empty string at first and now this is the content of your array

[""]

Later when you add other values and call getName() you will add the values on top of the already existing value

[""]
about 4 years ago · Juan Pablo Isaza Denunciar

0

Answer based on the comments.

the function call getName() at the end of the script calls the function right after it, even when the button hasn’t been pressed by the user. Therefore, it adds the empty value in the array since there is an empty value.

Just need to remove the getName().

const NameArr = [];
const IdArr = [];
Dict = {};

function getName() {
  const Input = document.getElementById("input").value;
  const list_names = Input.split(", ");

  for (let i of list_names) {
    NameArr.push(i);
    IdArr.push(i);
  };
  console.log(NameArr, IdArr);
};
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <script src="main.js" charset="utf-8" defer></script>
    <title></title>
  </head>
  <body>
    <form id="form" action="#" method="post">
      <input type="text" id="input">
      <button onclick="getName();">Get Data</button>
    </form>
  </body>
</html>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda