Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add two properties in nested array of object

I’ve an array of objects:

const arr = [
  {

    "topicId": 1,
    "topicName": "Topic 1",
    "topicIcon": {
      "name": "Caret",
      "class": "lmnicon lmnicon-caret-right"
    },
    "topicContent": "T1 topic content",

    "children": [
      {
        "topicId": 4,
        "topicName": "Topic 4",
        "topicIcon": {
          "name": "Caret",
          "class": "lmnicon lmnicon-caret-right"
        },
        "topicContent": "T4 topic content"

      },
      {
        "topicId": 5,
        "topicName": "Topic 5",
        "topicIcon": {
          "name": "Caret",
          "class": "lmnicon lmnicon-caret-right"
        },
        "children": [
          {
            "topicId": 6,
            "topicName": "Topic 6",
            "topicIcon": {
              "name": "Caret",
              "class": "lmnicon lmnicon-caret-right"
            }
          },
          {
            "topicId": 7,
            "topicName": "Topic 7",
            "topicIcon": {
              "name": "Caret",
              "class": "lmnicon lmnicon-caret-right"
            }
          },
          {
            "topicId": 8,
            "topicName": "Topic 8",
            "topicIcon": {
              "name": "Caret",
              "class": "lmnicon lmnicon-caret-right"
            }
          },
        ]
      }
    ]
  },
  {
    "topicId": 2,
    "topicName": "Topic 2",
    "topicIcon": {
      "name": "Caret",
      "class": "lmnicon lmnicon-caret-right"
    },
    "topicContent": "T2 topic content",

    "children": [
      {
        "topicId": 9,
        "topicName": "Topic 9",
        "topicIcon": {
          "name": "Caret",
          "class": "lmnicon lmnicon-caret-right"
        }

      },
      {
        "topicId": 10,
        "topicName": "Topic 10",
        "topicIcon": {
          "name": "Caret",
          "class": "lmnicon lmnicon-caret-right"
        }
      }
    ]
  }
]

const mapHeirarchy = list => list.flatMap(({ topicId, topicName, topicIcon,  children = [] }) => {
  const obj = {
    'topicId': topicId,
    'topicName': topicName,
    'topicIcon': topicIcon,
    'droppable': true, //add this
    'draggable': true  //add this
  }
  return [obj, ...mapHeirarchy(children)]
});
console.log(mapHeirarchy(arr))

I need to add this two properties droppable and draggable as true in objects as well as in nested childrens which might be ‘n’ levels deep.

Though I added the properties but I need to go back to the original ‘arr’ structure.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m not sure how can we go back or is there more correct way to achieve this.

>Solution :

Simply map instead of getting a flat array.

const
    array = [{ topicId: 1, topicName: "Topic 1", topicIcon: { name: "Caret", class: "lmnicon lmnicon-caret-right" }, topicContent: "T1 topic content", children: [{ topicId: 4, topicName: "Topic 4", topicIcon: { name: "Caret", class: "lmnicon lmnicon-caret-right" }, topicContent: "T4 topic content" }, { topicId: 5, topicName: "Topic 5", topicIcon: { name: "Caret", class: "lmnicon lmnicon-caret-right" }, children: [{ topicId: 6, topicName: "Topic 6", topicIcon: { name: "Caret", class: "lmnicon lmnicon-caret-right" } }, { topicId: 7, topicName: "Topic 7", topicIcon: { name: "Caret", class: "lmnicon lmnicon-caret-right" } }, { topicId: 8, topicName: "Topic 8", topicIcon: { name: "Caret", class: "lmnicon lmnicon-caret-right" } }] }] }, { topicId: 2, topicName: "Topic 2", topicIcon: { name: "Caret", class: "lmnicon lmnicon-caret-right" }, topicContent: "T2 topic content", children: [{ topicId: 9, topicName: "Topic 9", topicIcon: { name: "Caret", class: "lmnicon lmnicon-caret-right" } }, { topicId: 10, topicName: "Topic 10", topicIcon: { name: "Caret", class: "lmnicon lmnicon-caret-right" } }] }],
    droppable = true,
    draggable = true,
    mapHierarchy = list => list.map(({ children, ...o }) => ({
        ...o, 
        droppable, 
        draggable,
        ...(children && { children: mapHierarchy(children) })
    }));
    
console.log(mapHierarchy(array));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading