-3

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"
                     }
                  ]
               }
            ]
         }
      ]
   }
]
eupho
  • 1
  • 5
  • 12
    Have you tried anything? – Nitheesh Sep 15 '21 at 08:21
  • 1
    Why do you need map? This can be done using `Array.push`. – Nitheesh Sep 15 '21 at 08:23
  • Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available static and instance methods of [`Object`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). “I need this” isn’t a question. Where are your attempts? – Sebastian Simon Sep 15 '21 at 08:27
  • You are saying "I want to hammer a nail with a screwdriver". If you want to hammer a nail, you need a hammer, not a screwdriver. If you want to push a new object in an array, you need `push`, not `map`. – Jeremy Thille Sep 15 '21 at 08:31
  • Welcome to Stack Overflow! Visit the [help], take the [tour] to see what and [ask]. Please first ***>>>[Search for related topics on SO](https://www.google.com/search?q=javascript+add+object+array+site%3Astackoverflow.com)<<<*** and if you get stuck, post a [mcve] of your attempt, noting input and expected output using the [`[<>]`](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do) snippet editor. – mplungjan Sep 15 '21 at 08:32

3 Answers3

0

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);
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
0
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]}))
    }];
}, []);
eupho
  • 1
  • 5
  • Hi, can you explain more your solution, why it will fix his issue ? What it does exactly ? – Elikill58 Sep 16 '21 at 12:54
  • @eupho please explain your solution rather than just giving block of code – Rohit Ambre Sep 16 '21 at 13:03
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 16 '21 at 13:32
0

A simple way::

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 newPresentation = input.map(({ categories = [], ...rest }) => ({
        ...rest,
        categories: categories.map(({ id, categories = [], ...rest }) => ({
          ...rest,
          id,
          categories: [
            ...categories,
            { id: 'subcat3', name: 'subcat3', group: categories.flatMap(({ groups }) => groups) },
          ],
        })),
      }));
eupho
  • 1
  • 5