0

I have the following data that is an array of nested objects:

let sections = [
{
    name: "section1",
    id: 1,
    menus: [
      {
        id: 1,
        name: "menu1"
      },
      {
        id: 2,
        name: "menu2"
      }
      ]
  },
  {
    name: "section2",
    id: 2,
    menus: [
      {
        id: 3,
        name: "menu3"
      },
      {
        id: 4,
        name: "menu4"
      }
      ]
  }
]

I need an array of objects with properties from both nested objects. Expected Output:

[
{sectionId: 1, sectionName: "section1", menuId: 1, menuName: "menu1"},
{sectionId: 1, sectionName: "section1", menuId: 2, menuName: "menu2"},
{sectionId: 2, sectionName: "section2", menuId: 3, menuName: "menu3"},
{sectionId: 2, sectionName: "section2", menuId: 4, menuName: "menu4"}
]

Tried with below code:

let combined = sections.map(section => {
  let menus = section.menus.map(menu => {
    return{
      menuId: menu.id,
      menuName: menu.name,
      sectionName: section.name,
      sectionId: section.id
    }

  })  
  return menus
})

Actual Output:

[[{"menuId":1,"menuName":"menu1","sectionName":"section1","sectionId":1},{"menuId":2,"menuName":"menu2","sectionName":"section1","sectionId":1}],[{"menuId":3,"menuName":"menu3","sectionName":"section2","sectionId":2},{"menuId":4,"menuName":"menu4","sectionName":"section2","sectionId":2}]]

Getting Arrays of arrays of objects. Tried a lot using reduce and map but not getting the expected output. Any help is greatly appreciated.

0

3 Answers 3

0

You can flat the returned result using Array.prototype.flat():

combined.flat()

let sections = [
{
    name: "section1",
    id: 1,
    menus: [
      {
        id: 1,
        name: "menu1"
      },
      {
        id: 2,
        name: "menu2"
      }
      ]
  },
  {
    name: "section2",
    id: 2,
    menus: [
      {
        id: 3,
        name: "menu3"
      },
      {
        id: 4,
        name: "menu4"
      }
      ]
  }
]



let combined = sections.map(section => {
  let menus = section.menus.map(menu => {
    return{
      menuId: menu.id,
      menuName: menu.name,
      sectionName: section.name,
      sectionId: section.id
    }

  })  
  return menus;
});
console.log(combined.flat());

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

1 Comment

This is the easiest solution. Just added combined.flat() and it worked like a charm
0

You cold take Array#flatMap and map the inner array.

let sections = [{ name: "section1", id: 1, menus: [{ id: 1, name: "menu1" }, { id: 2, name: "menu2" }] }, { name: "section2", id: 2, menus: [{ id: 3, name: "menu3" }, { id: 4, name: "menu4" }] }],
    result = sections.flatMap(({ id: sectionId, name: sectionName, menus }) =>
        menus.map(({ id: menuId, name: menuName }) =>
            ({ sectionId, sectionName, menuId, menuName })
        )
    );

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

Comments

0

Please try this with lodash. Look into Flatten an nested array of objects using Lodash you will definitely get answer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.