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

231
Views
Creating Object Using Array Elements without overwriting Existing Keys

I trying to creating an object from an array that contains many arrays. The array element are to be nested keys of object for each other, only if the key has not be created initially. That is, it should not overwrite key and should also maintained key index.

Example is this -


const MainArray = [
                    {
                      key: ['name', 'dog', 'feature', 'hairy'],
                      value1:1 , 
                      value2:2
                     },
                    {
                      key: ['name', 'dog', 'eye', 'brown'],
                      value1:1 , 
                      value2:2
                     }, 
                    {
                      key: ['kind', 'human', 'class', 'man', 'height', 'tall'],
                      value1:'Mike' , 
                      value2:'John'
                     }, 
                     {
                      key: ['kind', 'human', 'class', 'woman', 'hobby'],
                      value1:'Cyling' , 
                      value2:'Tennis'
                     }, 

                ]


const requiredObject = 
                     {

                      name:{
                          dog :{
                            feature:{
                                   hairy :{value1:1, value2:2}
                            },
                            eye:{
                                brown:{value1:1, value2:2}
                             }
                           }
                        },
                        kind:{
                           human:{
                              class:{
                                   man:{
                                      height:{
                                        tall:{value1:'Mike', value2:'John'}
                                      }
                                   },
                                   woman:{
                                       hobby:{value1:'Cyling', value2: 'Tennis'}
                                   } 
                                }
                            }
                         }

                     }

How can I go from MainArray to requireObject

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

0

You can use array.reduce() to build a new object by traversing key arrays using another array.reduce(). Try:

const MainArray = [
    {
      key: ['name', 'dog', 'feature', 'hairy'],
      value1:1 , 
      value2:2
     },
    {
      key: ['name', 'dog', 'eye', 'brown'],
      value1:1 , 
      value2:2
     }, 
    {
      key: ['kind', 'human', 'class', 'man', 'height', 'tall'],
      value1:'Mike' , 
      value2:'John'
     }, 
     {
      key: ['kind', 'human', 'class', 'woman', 'hobby'],
      value1:'Cyling' , 
      value2:'Tennis'
     }
];


const result = MainArray.reduce((acc, cur) => {
    let { key, ...data } = cur;
    let lastIndex = key.length - 1;

    key.reduce((obj, k, index) => {
        obj[k] = obj[k] || (index === lastIndex ? {...data}  : {});
        return obj[k];
    }, acc);

    return acc;
}, {});

console.log(result);

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!