JavaScript Why is there not one array with all the items in it, but an array for every item?

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)
  }

}
Answer:1

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)
}
Answer:2

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)
}
Answer:3

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)
  }
}
Answer:4

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 = ...

  1. getelementsbyclassname javascript only

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 }, ...

  1. array from properties file
  2. array properties in javascript
  3. array properties in c#
  4. array properties in java
  5. array properties in js
  6. array properties in python
  7. array properties in php
  8. array properties in swift
  9. array properties in vba
  10. array properties in data structure
  11. array properties in typescript
  12. array properties in angular 2
  13. array properties in angular 6
  14. array properties in vb.net
  15. array properties in matlab
  16. array properties in powershell
  17. array in properties file spring
  18. array in properties file java
  19. array of properties
  20. array in properties spring

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 }); ...

  1. javascript promise returned is ignored
  2. javascript promise returned
  3. js promise returned is ignored
  4. js return promise
  5. javascript handle returned promise