Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
How do I assign an array to another array?

Here is my code

export default class Application extends EventEmitter {
  constructor() {
    super();

    this.config = config;
    this.data = {
      planets: [planetData]
    };

    this.init();

    let url;
    let count;
    let planetData = []
    let promises = [];

    //turn through the pages
    for (let p = 1; p < 7; p++) {
      url = `https://swapi.boom.dev/api/planets?page=${p}`;

      //fetch data
      promises.push(fetch(url).then(res => res.json())
        .then(data => {

          //push data to array
          for (let i = 0; i < data.results.length; i++) {
            planetData = planetData.concat(data.results[i]);
          }

        }));
    }

    Promise.all(promises)
      .then(() => {
        console.log(planetData.length, '=>', planetData);
      })
  }

I need help assigning the planetData array to this.data{}. I tried the following this.data{ planets: [planetData], but it gave an error "Cannot access 'planetData' before initialization", which was expected. My syntax is also probably wrong, but I'm really new to JS.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You should assign the variable after the planetData array has been populated.

Perhaps you can try something like this:

Promise.all(promises)
    .then(() => {
      console.log(planetData.length, '=>', planetData);
    })
    .then(() => {
        this.data.planets = planetData;
    })
about 4 years ago · Juan Pablo Isaza Report

0

You can assign planetData to the key planets later in your code using:

this.data.planets = planetData;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!