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");
});
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.
– user27585
@Erick updated his answer, maybe he’d be interested.
– Tobias Mesquita
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.
– user27585
@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
– Tobias Mesquita
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?
– user27585
@Eric with a Sort:
items = items.sort(function (itemA, itemB) { return itemB.positivo - itemA.positivo; })
– Tobias Mesquita
Genio rs, thank you.
– user27585
Vue.js ++ :) I am also infected
– Sergio
@Sergio if you are also infected, then I am calmer ;D
– Tobias Mesquita
By the way, suggestion:
people.innerHTML = items.map(({name}) => \
<li> ${name} </li>`). Join('');`– Sergio
@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.
– Tobias Mesquita
@Tobiasmesquita is, the syntax gets a little strange at first, but helps to save the loop
for
– Sergio
I think I’d end up using the
item => item.name
instead of({name}) => name
(which equals).– Tobias Mesquita