I want to push all the item-data into one array. However, when I try, it just makes an array for every item.
async function final() {
const response = await fetch('/api');
const data = await response.json();
console.log(data)
for (item of data) {
const num = item.atomicNumber;
const arr = []
arr.push(num)
console.log(arr)
}
}
Because you are creating a new array inside the loop. Analyzing your own code step by step and understanding what every line does is mandatory to be a good developer :-). Let's analyze it:
async function final() {
const response = await fetch('/api');
const data = await response.json();
console.log(data)
// Until here everything is fine
// You define a loop which will run from this point each time
for (item of data) {
const num = item.atomicNumber;
// You are creating a new array inside the loop. The loop, as its name says, will run one time per item.
const arr = []
arr.push(num)
console.log(arr)
}
}
To fix this, just move the array outside the loop, so it only runs one time:
async function final() {
const response = await fetch('/api');
const data = await response.json();
console.log(data)
// We create the array outside the loop
const arr = []
// Then define a loop which will run from this point each time
for (let item of data) { // Don't forget to define variables with the proper keyword (in this case, "let" is enough).
const num = item.atomicNumber;
arr.push(num)
}
// We log the array when the loop has ended, so it logs only one time
console.log(arr)
}
you need to put your empty array const arr = []
out of the loop, otherwise you're re-declaring the array at every iteration.
async function final() {
const response = await fetch('/api');
const data = await response.json();
console.log(data)
const arr = []
for (item of data) {
const num = item.atomicNumber;
arr.push(num)
}
console.log(arr)
}
You're re-declaring arr
in every iteration of the loop. Move the declaration out of it, and just push into it every iteration:
async function final() {
const response = await fetch('/api');
const data = await response.json();
console.log(data)
const arr = [] // arr declared outside the loop
for (item of data) {
const num = item.atomicNumber;
arr.push(num) // but used inside it
console.log(arr)
}
}
I have html like this: <div class="item"> <img class="item-image" src="${item.getImage()}"/> <p>${item.getName()}</p> </div> and Javascript: var classname = ...
I have html like this: <div class="item"> <img class="item-image" src="${item.getImage()}"/> <p>${item.getName()}</p> </div> and Javascript: var classname = ...
I have below array how to get list of names where the age is greater than 18. so the out put should be: ["Anna", "Bob"] friends = [{ name: 'Anna', books: ['Bible', 'Harry Potter'], age: 21 }, ...
I have below array how to get list of names where the age is greater than 18. so the out put should be: ["Anna", "Bob"] friends = [{ name: 'Anna', books: ['Bible', 'Harry Potter'], age: 21 }, ...
When I click more button my div 1 is replacing div 2. Most of the time div2 contains lots of text, and requires users to scroll down in order to read the entire post. The problem is that by clicking ...
When I click more button my div 1 is replacing div 2. Most of the time div2 contains lots of text, and requires users to scroll down in order to read the entire post. The problem is that by clicking ...
I have some squares sliding down the page, in a chained promise fashion: https://jsfiddle.net/u4x0qwfo/3 The code is: new Promise(function(resolve, reject) { $("#shape").css({ top: 100 }); ...
I have some squares sliding down the page, in a chained promise fashion: https://jsfiddle.net/u4x0qwfo/3 The code is: new Promise(function(resolve, reject) { $("#shape").css({ top: 100 }); ...