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

73
Views
Modify array of the nested objects by it's nested object's properties

I have the following structure in my array.

const data = [
  {
    name: 'Book_1',
    value: {
      eng: 'english_book_1',
      sp: 'spanish_book_1'
    }
  },
  {
    name: 'Book_2',
    value: {
      eng: 'english_book_2',
      sp: 'spanish_book_2'
    }
  }
];

And trying to get a structure like this:

[
  {
    eng: {
      Book_1: 'english_book_1',
      Book_2: 'english_book_2'
    }
  },
  {
    sp: {
      Book_1: 'spanish_book_1',
      Book_2: 'spanish_book_2'
    }
  }
];

So, the array should have language keys and nested book names with values in it. I tried a couple of things but even close.

const modified = [];
const object = {};
data.forEach(el => {
  Object.entries(el.value).forEach(([name, value]) => {
    if (object['name']) {
    }
    modified.push(object);
  });
});
```

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

0

You could reduce the array by taking an object for the language's objects.

const
    data = [{ name: 'Book_1', value: { eng: 'english_book_1', sp: 'spanish_book_1' } }, { name: 'Book_2', value: { eng: 'english_book_2', sp: 'spanish_book_2' } }],
    result = Object.values(data.reduce((r, { name, value }) => {
        Object
            .entries(value)
            .forEach(([k, v]) => (r[k] ??= { [k]: {} })[k][name] = v);
        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!