0

How do I extract the array in an array and structure it to be one whole array?

Input:

const input = [{
    categoryName: "Chinese food",
    tabs: [{
        header: "Chicken Rice",
        content: "Hainanese chicken rice is a dish of poached chicken and seasoned rice"
      },
      {
        header: "Dim sum",
        content: "large range of small Chinese dishes that are traditionally enjoyed in restaurants"
      }
    ]
  },
  {
    categoryName: "Italian food",
    tabs: [{
      header: "Pizza",
      content: "Dish of Italian origin consisting of a usually round"
    }]
  },
]

Output (Need to extract all the headers out to become this array)

const output = [
  {
    "categoryName": "Chinese food",
    "header": "Chicken Rice",
  },
  {
    "categoryName": "Chinese food",
    "header": "Dim sum",
  },
  {
    "categoryName": "Italian food",
    "header": "Pizza"
  },
]

tried this but i can't seem to add categoryName inside

const getHeadersWithId = arr => (
  arr.flatMap(                    // iterate over 'input' array and 'flat'-ten the result
    ({tabs}) => (tabs.map(        // de-structure 'tabs' & iterate over it
      ({header}) => ({header})    // de-structure 'header' & retain it to intermediate-result array
    ))
  ).map(
    ({header}, idx) => ({         // iterate with index 'idx'
      id: idx+1,                  // generate the 'id' and store
      header                      // header from intermediate-result array element
    })
  )
);

This is what I got when I run this chunk of code

[
  {
    "id": 1,
    "header": "Chicken Rice"
  },
  {
    "id": 2,
    "header": "Dim sum"
  },
  {
    "id": 3,
    "header": "Pizza"
  }
]
4
  • 2
    What are your ideas about this? What have you tried Commented Apr 5, 2022 at 6:43
  • Hi @TusharShahi, I tried the code above. Updated the question to include what I tried. Commented Apr 5, 2022 at 6:45
  • If you only want categoryName and header, what's the the id property in your code? Commented Apr 5, 2022 at 6:50
  • TL;DR on the duplicate? You want input.flatMap(({ categoryName, tabs }) => tabs.map(({ header }) => ({ categoryName, header }))) Commented Apr 5, 2022 at 6:54

1 Answer 1

1

You don't need the second map() in my opinion. Just use the categoryName and pass it into your inner map().

Building up on your code:

const input = [{
    categoryName: "Chinese food",
    tabs: [{
        header: "Chicken Rice",
        content: "Hainanese chicken rice is a dish of poached chicken and seasoned rice"
      },
      {
        header: "Dim sum",
        content: "large range of small Chinese dishes that are traditionally enjoyed in restaurants"
      }
    ]
  },
  {
    categoryName: "Italian food",
    tabs: [{
      header: "Pizza",
      content: "Dish of Italian origin consisting of a usually round"
    }]
  },
]

const getHeadersWithId = arr => (
  arr.flatMap(                    // iterate over 'input' array and 'flat'-ten the result
    ({tabs,categoryName}) => tabs.map(       // de-structure 'tabs' & iterate over it
      ({header}) => ({header,categoryName})    // de-structure 'header' & retain it to intermediate-result array
    )
  )
);

console.log(getHeadersWithId(input));

Sign up to request clarification or add additional context in comments.

1 Comment

Yes correct. Just copy pasted OP's code. Editing

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.