Need a way to get array of objects with mixed properties from an array of nested objects in JavaScript

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.

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());

I've got an array: myArray = [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}, etc.] I'm unable to change the structure of the array. I'm being passed an id of 45

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; }

JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. The item argument is a reference to the current element in the array as filter checks it against the condition. Using dot notation the nested objects' property(car) is accessed.

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

Defining an array and accessing an array element are two different things. So don’t mix them up. To define an array you can use array() or for PHP >=5.4 [] and you assign/set an array/-element. While when you are accessing an array element with [] or {} as mentioned above you get the value of an array element opposed to setting an element.

Copyright © 2015-2016 by Object Computing, Inc. (OCI) Immutability All rights reserved Persistent Data Structures Wikipedia says “a data structure that always

For example, document. We need to get the superhero having the power 4/s. filter - Filter a data stream using a predicate expression. In this blog post, we are going to see the ways we can convert Object to/from Array in javascript/Typescript/Angular applications. In the previous tutorial, we retrieved typed data objects over the network.

Instance properties with getters returning wrong values: JDK-8072002: core-libs: javax.script: The spec on javax.script.Compilable contains a typo and confusing inconsistency: JDK-8073846: core-libs: javax.script: Javascript for-in loop returned extra keys: JDK-8059411: core-libs: javax.sql: RowSetWarning does not correctly chain warnings: JDK

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