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

92
Views
How to convert string array into custom nested object

I am trying to convert a string array into single nested object suggest a best way to do that,

For example, If I had a single element in an array

Input:

var keys = ["A"];

It should be like this,

Output:

{
    and: {
        tags: {
            contains: "A"
        }
    }
}

And if I add a second element for example,

Input:

var keys = ["A","B"];

It should add as a nested object like this,

Output:

{
    and: {
        tags: {
            contains: "A"
        },
        and: {
            tags: {
                contains: "B"
            }
        }
    }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can try to use Array.reduceRight function(Thanks Drew Reese's suggestion):

const nest = arr => arr.reduceRight((acc, cur) => ({ and: { tags: { contains: cur }, ...acc } }), {});

console.log(nest(['A']));
console.log(nest(['A', 'B']));
console.log(nest(['A', 'B', 'C']));

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!