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

172
Views
each value of array is a object subproperty of the previous array value

so I have an array ['banana', 'apple', 'grape']

how can I transform into the object below?

{
   banana: {
       apple: {
           grape: { }
       }
   }
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use reduceRight() to recursively build the result object:

const data = ['banana', 'apple', 'grape'];

const result = data.reduceRight((a, v) => ({ [v]: a }), {});

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

You can simply loop over given value and keep track of previously added object

let arr = ['banana', 'apple', 'grape']


function createObj(arr) {
  let obj = {}
  let temp = obj

  for (let key of arr) {
    temp[key] = {}
    temp = temp[key]
  }
  return obj
}

console.log(createObj(arr))

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!