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

124
Views
Divide S3 prefix to list of object in Javascript

Example, I have a path(prefix) like this: A/B/C/

I want to get bellow list:

[{name:"A",path:"A/"},
 {name:"B",path:"A/B/",
 {name:"C",path:"A/B/C/"
]

I can split the path to a array, then loop the array to build the new list of object. But in my mind, I just know there should be a simple and smarter way to achieve this by using reducer, but just stuck here.

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

0

You're right that you could use a reducer. Something like this:

const str = "A/B/C/"

const arr = str.split("/").filter(Boolean).reduce((acc, name) => {
  const path = [...acc.map(o => o.name), name].join("/") + "/"
  return [...acc, { name, path }]
}, [])

console.log(arr)

about 4 years ago · Juan Pablo Isaza Report

0

My odd solution, but I think sdgluck's is the cleanest answer.

arr = "A/B/C/"
    .split("/")
    .filter(e => e)
    .map((e, i, a) => {
        a2 = a.filter((el, ix) => {
            if (ix <= i) return el;
        });

        return  {[e] : a2.join("/")};
    });
about 4 years ago · Juan Pablo Isaza Report

0

You can solve this with map, but maybe not as cleanly as you were anticipating:

const result = 'A/B/C'
  .split('/')
  .filter(x => x)
  .map((name, i, arr) => {
    const prev = arr[i - 1];
    return prev
      ? { name, path: `${prev.name}${name}/` }
      : { name, path: `${name}/` };
  });
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!