Home > Software engineering >  Alter array of object using map in javascript
Alter array of object using map in javascript

Time:09-17

I have an array of object: Note: I want to use map javascript method to add new object inside inner categories which will contain new object subcat3 which further contains all group object inside "group".

[
   {
      "id":"presentation",
      "categories":[
         {
            "id":"cat1",
            "categories":[
               {
                  "id":"subcat1",
                  "name":"subcat1",
                  "group":[
                     {
                        "id":"group1",
                        "name":"group1"
                     },
                     {
                        "id":"group2",
                        "name":"group2"
                     }
                  ]
               },
               {
                  "id":"subcat2",
                  "name":"subcat2",
                  "group":[
                     {
                        "id":"group3",
                        "name":"group3"
                     }
                  ]
               }
            ]
         }
      ]
   }
]

And the result I need::

[
   {
      "id":"presentation",
      "categories":[
         {
            "id":"cat1",
            "categories":[
               {
                  "id":"subcat1",
                  "name":"subcat1",
                  "group":[
                     {
                        "id":"group1",
                        "name":"group1"
                     },
                     {
                        "id":"group2",
                        "name":"group2"
                     }
                  ]
               },
               {
                  "id":"subcat2",
                  "name":"subcat2",
                  "group":[
                     {
                        "id":"group3",
                        "name":"group3"
                     }
                  ]
               },
               {
                  "id":"subcat3",
                  "name":"subcat3",
                  "group":[
                     {
                        "id":"group1",
                        "name":"group1"
                     },
                     {
                        "id":"group2",
                        "name":"group2"
                     },
                     {
                        "id":"group3",
                        "name":"group3"
                     }
                  ]
               }
            ]
         }
      ]
   }
]

CodePudding user response:

If you want to use map for sure, then you can follow below approach.

Logic

  • Map throgh the nodes in input array
  • Map through categories in each node
  • Map through categories in each category
  • Map through each group in that category.

From the array generated bu that, flat that array with level 3 and push to your desired node.

const input = [{"id":"presentation","categories":[{"id":"cat1","categories":[{"id":"subcat1","name":"subcat1","group":[{"id":"group1","name":"group1"},{"id":"group2","name":"group2"}]},{"id":"subcat2","name":"subcat2","group":[{"id":"group3","name":"group3"}]}]}]}];
const newCat = input.map((ipCategory) => ipCategory.categories.map((category) => category.categories.map((innerCategory) => innerCategory.group.map((group) => group))));
const groups = newCat.flat(3);
input[0].categories[0].categories.push({
  id:"subcat3",
  name:"subcat3",
  group: groups
});
console.log(input);

CodePudding user response:

const input = [{"id":"presentation","categories":[{"id":"cat1","categories":[{"id":"subcat1","name":"subcat1","group":[{"id":"group1","name":"group1"},{"id":"group2","name":"group2"}]},{"id":"subcat2","name":"subcat2","group":[{"id":"group3","name":"group3"}]}]}]}];

const newInput = input.reduce((a,c) => {
    const modified = c.categories.map((category, id) => {
    return({
        ...category,
        'categories': [
            ...category.categories,
            { id: 'subcat3', name: 'subcat3', groupings: category.categories.flatMap(({ group }) => group) }
        ]
       });
    });
    return [{
        ...c,
        categories: c.categories.map((i, ind) => ({ ...modified[ind]}))
    }];
}, []);
  • Related