mount HTML element with vanilla JS, cannot list all JSON elements

Asked

Viewed 38 times

1

Where is the error in my code?

I’m not getting to list all the names each in one <li> different, in my console.log(items[i].name) all JSON names appear, but when creating the html element it only takes the last name.

const myList = fetch('people.json')
.then(response => response.json())
.then(result => {
  const items = result.data;

  for(let i = 0; i < items.length; i++) {
    let people = document.getElementById("people");

    console.log(items[i].name)
    
    const markup = `
      <li>
        ${items[i].name}
      </li>
    `;

    people.innerHTML = markup;
  }
})
.catch(err => {
  console.log("Erro");
});

1 answer

1


you forgot a +.

people.innerHTML += markup;

in any case, I suggest at least that you remove the .innerHTML = markup of your bond, this operation is very costly.

let markup = "";
let people = document.getElementById("people");
for(let i = 0; i < items.length; i++) {
  console.log(items[i].name);
  markup += `
    <li>
      ${items[i].name}
    </li>
  `;
}
people.innerHTML = markup;

And finally I’m in a Vuejs-using vibe for everything ;D

var app = new Vue({
  el: '#app',
  data: {
    passo: "Iniciando a aplicação",
    nomes: [ "Fulano", "Ciclano", "Beltrano" ]
  }
});

// inserir novo nome apos 2 segundos.
window.setTimeout(function () {
  app.passo = "inserindo novo nome apos 2 segundos.";
  app.nomes.push("Toby Mosque");
}, 2000);

// atualizar toda a lista apos 4 segundos
window.setTimeout(function () {
  app.passo = "atualizando toda a lista apos 4 segundos.";
  app.nomes = [ "Eric", "Toby Mosque" ]
}, 4000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.4/vue.min.js"></script>
<div id="app">
  <p>passo: {{passo}}</p>
  <ul v-for="(nome, indice) in nomes">
    <li>{{indice}} - {{nome}}</li>
  </ul>
</div>

  • Damn it, I’m vaccinating for an extra, thank you very much. I forgot that the way I was when creating one it erases the previous one and so the last name of the list appears. VLWW I will follow your tip too.

  • 1

    @Erick updated his answer, maybe he’d be interested.

  • Cool, I wanted to start with Vue but I took an angle course 4 rescente then I will try to play with it. But in this specific case it has to be only JS even. But with ctz will serve me to study your example. Thanks even.

  • 1

    @Erick what I think is cool about Vue, is that you can use it to solve small and large problems, as well as using it in existing systems, and of course you can gradually adopt and learn. With the angle, the hole is lower ;D

  • in this json where I have the names, I have a so-called positive item that has a value, what is the best way in JS to list first who has the highest value in the positive field? maybe a . map?

  • 1

    @Eric with a Sort: items = items.sort(function (itemA, itemB) { return itemB.positivo - itemA.positivo; })

  • Genio rs, thank you.

  • Vue.js ++ :) I am also infected

  • 1

    @Sergio if you are also infected, then I am calmer ;D

  • 1

    By the way, suggestion: people.innerHTML = items.map(({name}) => \<li> ${name} </li>`). Join('');`

  • @Sergio Arrow Function with attribution of the arguments from the name of the properties of the object, seems to me a little confused ;D. To be sinsero, is the first time I see the gender.

  • @Tobiasmesquita is, the syntax gets a little strange at first, but helps to save the loop for

  • 1

    I think I’d end up using the item => item.name instead of ({name}) => name (which equals).

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.