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

228
Views
how to assign an multiple id's to an object with in an array corresponding to related collection with in a loop?

I have the below logic, my current implementation works fine the last puzzle missing is the parent it, I want to able to assign the same parent id to every similar set of items the current implementation sets the same grandParentId to all items instead to the related collection, in other works items fall under a1 must have the same grandParent and under b2 a different grandParent, I am using uuid library to populate ids.

// arr
let crossCheckArr = [{name: 'a'}, {name: 'b'}, {name: 'c'}, {name: 'd'}, {name: 'e'}]
  
// Object
const rawObj= {
    a1: [
      { name: 'a' },
      { name: 'b' },
      { name: 'c' },
    ],
    b2: [{ name: 'd'}],
    d3: [{ name: 'e'}],
  };

// Consolidation Logic
  const grandParentId = uuidv4();
    const final = Object.entries(days).reduce((obj, [key, value], i) => {
      const parentId = uuidv4();
      return {
        ...obj,
        [key]: crossCheckArr.filter((el) => {
          if (value.map((name) => name.name).includes(el.name)) {
            return Object.assign(el, {
              id: uuidv4(),
              parentId: parentId,
              grandParentId: grandParentId,
            });
          }
        }),
      };
    }, {});
// Final     
//  {
//  a1: [
//    { name: 'a' , parentId : 11, grandParentId : 123},
//     { name: 'b', parentId : 11, grandParentId : 123},
//     { name: 'c', parentId : 11, grandParentId : 123},
//    ],
//    b2: [{ name: 'd',parentId : 22, grandParentId : 456}],
//    d3: [{ name: 'e',parentId : 33, grandParentId : 789}],
//  };

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!