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

218
Visualizações
Creating and showing dictionary from two arrays in js not working

I know there are many posts out there that answer my question, but for some reason that I cannot figure out, I never end up with the desire result.

Here is my code:

const NameArray = [];
const IdArray = [];
let Dict = {};

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

  for (let i of list_names) {
    NameArray.push(i);
    IdArray.push(i);
  };
  console.log(NameArray, IdArray);
};

function CreateDict() {
  NameArray.forEach((key, i) => Dict[key] = IdArray[i]);
  console.log(Dict);
};

CreateDict();
<!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 almost sure this simple code should work properly, but in my console log I do not see the array. I’m working with Firefox, the latest version.

what I get is the following:

Object {  }
​
<prototype>: Object { … }
​​
__defineGetter__: function __defineGetter__()
​​
__defineSetter__: function __defineSetter__()
​​
__lookupGetter__: function __lookupGetter__()
​​
__lookupSetter__: function __lookupSetter__()
​​
__proto__: 
​​
constructor: function Object()
​​
hasOwnProperty: function hasOwnProperty()
​​
isPrototypeOf: function isPrototypeOf()
​​
propertyIsEnumerable: function propertyIsEnumerable()
​​
toLocaleString: function toLocaleString()
​​
toString: function toString()
​​
valueOf: function valueOf()
​​
<get __proto__()>: function __proto__()
​​
<set __proto__()>: function __proto__()

It is problematic because I cannot debug, and I don’t even know how to access the key value pairs then...

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

0

As I can see you have imported script file in your html file. This makes your CreateDict() function run even before you type in input field. CreateDict() function should only be run only after once getName() is invoked. Ideal way will be :

const NameArray = [];
const IdArray = [];
let Dict = {};

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

  for (let i of list_names) {
    NameArray.push(i);
    IdArray.push(i);
  };
  console.log(NameArray, IdArray);
  CreateDict();
};

function CreateDict() {
  NameArray.forEach((key, i) => Dict[key] = IdArray[i]);
  console.log(Dict);
};
<!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 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