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

91
Views
How to create an array of objects with one of the properties being an array from a single array

I got this array:

[
name1, 
email1@name1.com,
email2@name1.com,
email3@name1.com,
name2,
email1@name2.com,
name3,
name4,
email1@name4.com,
email2@name4.com,
email3@name4.com,
email4@name4.com,
email5@name4.com,
]

From that array I need to parse it and create an array like the following:

[
{ dlName: < element without @ > , members: [<elements with @ >]}, 
{ dlName: < element without @ > , members: [<elements with @ >]}, 
{ dlName: < element without @ > , members: [<elements with @ >]}
]

example:

[
  HR,
  someonefks@name.com,
  fjkdafaldksjh@name.com,
  sfafafads@name.com,
  cyber,
  fakdjfnakj@name.com,
  fdsfasfasds@name.com,
  accessibility,
  accounting,
   dasaasas@name.com,
   klhdaflkaa@name.com
]

output:

[
{ dlName: HR , members: [someonefks@name.com,fjkdafaldksjh@name.com,sfafafads@name.com,]}, 
{ dlName: cyber , members:[fakdjfnakj@name.com,fdsfasfasds@name.com,]}, 
{ dlName: accesibility , members: []},
{ dlName: accounting , members: [dasaasas@name.com,klhdaflkaa@name.com]}
]

How can I get this output, everything I tried does not work for me needs.

Thanks in advance

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

0

You could reduce the array by having a look to the strings.

If you got an '@' add the string to the last object to members, otherose add a new object to the result set.

const
    data = ['name1', 'email1@name1.com', 'email2@name1.com', 'email3@name1.com', 'name2', 'email1@name2.com', 'name3', 'name4', 'email1@name4.com', 'email2@name4.com', 'email3@name4.com', 'email4@name4.com', 'email5@name4.com'],
    result = data.reduce((r, v) => {
        if (v.includes('@')) r[r.length - 1].members.push(v);
        else r.push(({ dlName: v, members: [] }));
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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!