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

161
Visualizações
I try to access the array elements but get undefined

I cannot access the elements of the array. I have not put the whole code because it is too long but I explain what I have done.

I have an array results of the following type:

results [i] = myArr.Items [i]

or so done:

{3: {...}, 4: {...}, 9: {...}}
3: {type: 'Image', id_a: '123456', value: 'moto_guzzi_v100_mandello.jpg', number: 3, id: 3}
4: {type: 'Text', id_a: '123456', value: 'The star of the Piaggio group stand is without doubt ... Discover all her secrets with our video', number: 2, id: 2}
9: {type: 'Text', id_a: '123456', value: 'The V100 Mandello celebrates the 100th anniversary of the house and… ovative. Let's find out together with our video. ', Number: 4, id: 4}

and I'm trying to recover the items as shown in the code below:

Object.entries(results).forEach(function (item) {               
                    let child = document.createElement("tr");
                    child.innerHTML = `
                    <td>${item.number}</td>
                    <td>${item.type}</td>
                    <td>${item.value}</td>
                    table.appendChild(child);
                })
   <table id="my-table" width="90%">
                    <tr>
                        <th>Number</th>
                        <th>Type</th>
                        <th>Value</th>
                    </tr>
            
                </table>

I just get the value undefined for each value.

Can anyone kindly explain to me the correct way to access these values?

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

0

Don't use Object.entries. The correct form of iterating over your array is:

for (const i in results) {
    const item = results[i];    
    let child = document.createElement("tr");
    child.innerHTML = `
    <td>${item.number}</td>
    <td>${item.type}</td>
    <td>${item.value}</td>
    table.appendChild(child);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Object.entries() makes your object list iterable, but if you console.log each item you'll see its an array with 2 entries - the index (0) and the data (1)

let results = {
  3: {
    type: 'Image',
    id_a: '123456',
    value: 'moto_guzzi_v100_mandello.jpg',
    number: 3,
    id: 3
  },
  5: {
    type: 'Text',
    id_a: '123456',
    value: 'The star of the Piaggio group stand is without doubt ... Discover all her secrets with our video',
    number: 2,
    id: 2
  },
  8: {
    type: 'Text',
    id_a: '123456',
    value: 'The V100 Mandello celebrates the 100th anniversary of the house and… ovative. Let\'s find out together with our video. ',
    Number: 4,
    id: 4
  }
};

Object.entries(results).forEach(item => {
  item = item[1];
  let child = document.createElement("tr");
  child.innerHTML = `
                    <td>${item.number}</td>
                    <td>${item.type}</td>
                    <td>${item.value}</td>`
  document.querySelector('#my-table').appendChild(child);
})
<table id="my-table" width="90%" border='1'>
  <tr>
    <th>Number</th>
    <th>Type</th>
    <th>Value</th>
  </tr>

</table>

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