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

136
Views
Js how to set an item at a nested index in an empty array

I have this array that has to be filled with objects. Each object has a slug with 4 sections divided by "-" (for example: test-dx-ef-1) and depending on it I have to place them in an array at a specific index named with the slug section.

I tried looping all the inputs and directly setting the object like so:

var data = []
objects.forEach((obj) => {

    var slug = obj.slug.split('-')
    data[slug[0]][slug[1]][slug[2]][slug[3]] = obj

}

But it didn't work, as apparently, even if data[slug[0]] can be created, data[slug[0]][slug[1]] results undefined and an error is thrown.

Is there a way in which I can accomplish this?

about 4 years ago · Juan Pablo Isaza
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!