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

321
Views
How can I extract elements of an array inside an array to this array?

How can I extract elements of an array inside an array to this array?

What I have is this:

const tableData = [
    {
        display: '2022-03',
        column: 'data',
        detailList: [
            {
                title: 'Quantity',
                value: 1,
                sourceId: null,
            },
            {
                title: 'Price',
                value: 2,
                sourceId: null,
            },
            {
                title: 'Weight',
                value: 3,
                sourceId: null,
            },
            {
                title: 'income',
                value: 4,
                sourceId: null,
            },
            
        ],
    },
    {
        display: '2022-02',
        column: 'data',
        detailList: [
            {
                title: 'Quantity',
                value: 7,
                sourceId: null,
            },
            {
                title: 'Price',
                value: 6,
                sourceId: null,
            },
            {
                title: 'Weight',
                value: 5,
                sourceId: null,
            },
            {
                title: 'income',
                value: 4,
                sourceId: null,
            },
            
        ],
    },
];      

Now I want it to become this:

res = [
    {
        title: '2022-03',
        'Quantity': '1',
        'Price': '2',
        'Weight': '3',
        'income': '4',      
    },
    {
        title: '2022-02',
        'Quantity': '7',
        'Price': '6',
        'Weight': '5',
        'income': '4',
    },
];      

What's the best way to do it?

I'm trying to create a new array outside, and use two .map() to make it. But it looks not readable.

Can somebody give me a better solution that uses some ES6 features that makes it look more readable.

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

0

You should assign the result of map() directly to res, rather than calling push() inside the map.

You can use Object.fromEntries() to create the object from the inner map.

Destructuring and spread syntax can simplify the way you access the properties and build the resulting object.

const tableData = [{
    display: '2022-03',
    column: 'data',
    detailList: [{
        title: 'Quantity',
        value: 1,
        sourceId: null,
      },
      {
        title: 'Price',
        value: 2,
        sourceId: null,
      },
      {
        title: 'Weight',
        value: 3,
        sourceId: null,
      },
      {
        title: 'income',
        value: 4,
        sourceId: null,
      },

    ],
  },
  {
    display: '2022-02',
    column: 'data',
    detailList: [{
        title: 'Quantity',
        value: 7,
        sourceId: null,
      },
      {
        title: 'Price',
        value: 6,
        sourceId: null,
      },
      {
        title: 'Weight',
        value: 5,
        sourceId: null,
      },
      {
        title: 'income',
        value: 4,
        sourceId: null,
      },

    ],
  },
];

let res = tableData.map(({
  display,
  detailList
}) => ({
  title: display,
  ...Object.fromEntries(detailList.map(({
    title,
    value
  }) => [title, value === null ? '-' : value]))
}));

console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

const res = tableData.map(({ display, detailList}) => {
  const obj = {
    title: display,
  }
  detailList.forEach(el => obj[el.title] = el.value);
  return obj;
})
about 4 years ago · Juan Pablo Isaza Report

0

This may be one possible solution to achieve the desired objective.

Code Snippet

const transformArray = arr => arr.map(  // iterate using "map"
  ({display : title, detailList}) => ({ // de-structure, and rename
    title,
    ...(
      detailList.reduce(                // iterate using "reduce"
        (fin, {title, value}) => ({     // de-structure to access title, value
          ...fin,
          [title]: value                // may add "value" is null check here
        }),
        {}                              // the "fin" object is initialized to empty
      )
    )
  })
);

const tableData = [
    {
        display: '2022-03',
        column: 'data',
        detailList: [
            {
                title: 'Quantity',
                value: 1,
                sourceId: null,
            },
            {
                title: 'Price',
                value: 2,
                sourceId: null,
            },
            {
                title: 'Weight',
                value: 3,
                sourceId: null,
            },
            {
                title: 'income',
                value: 4,
                sourceId: null,
            },
            
        ],
    },
    {
        display: '2022-02',
        column: 'data',
        detailList: [
            {
                title: 'Quantity',
                value: 7,
                sourceId: null,
            },
            {
                title: 'Price',
                value: 6,
                sourceId: null,
            },
            {
                title: 'Weight',
                value: 5,
                sourceId: null,
            },
            {
                title: 'income',
                value: 4,
                sourceId: null,
            },
        ],
    },
];


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

Explanation

Inline comments explain the steps involved.

NOTES

This solution uses:

  • '.map()`
  • .reduce()
  • de-structure and rename (display is renamed as title
  • arrow functions
  • ... spread to perform shallow-copy
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!