1

I have the following array:

     const arr = [  
   {  
      id:"aaa",
      name:"aaa",
      type:"director",
      nodes:[  
         {  
            type:"manager",
            id:"111",
            name:"111"
         }
      ]
   },
   {  
      id:"aaa",
      name:"bbb",
      type:"director",
      nodes:[  
         {  
            type:"manager",
            id:"222",
            name:"222"
         }
      ]
   },
   {  
      id:"aaa",
      name:"aaa",
      type:"director",
      nodes:[  
         {  
            type:"manager",
            id:"333",
            name:"333"
         }
      ]
   },
   {  
      id:"aaa",
      name:"bbb",
      type:"director",
      nodes:[  
         {  
            type:"manager",
            id:"444",
            name:"444"
         }
      ]
   }
]

desired output:

        const arr = [  
   {  
      id:"aaa",
      name:"aaa",
      type:"director",
      nodes:[  
         {  
            type:"manager",
            id:"111",
            name:"111"
         },
         {  
            type:"manager",
            id:"333",
            name:"333"
         }
      ]
   },
   {  
      id:"aaa",
      name:"bbb",
      type:"director",
      nodes:[  
         {  
            type:"manager",
            id:"222",
            name:"222"
         },
         {  
            type:"manager",
            id:"444",
            name:"444"
         }
      ]
   }

I am trying to merge the value of the array of objects of nodes that have the same key-value(id, name, and type) pairs in the above array to the desired output.

I have tried the following but in vain with the arr.reduce, but in vain:

arr.reduce((acc, el) => {
      var existEl = acc.find(e => e.nodes.id === el.nodes.id);
      if (existEl) {   
        existEl = el;
      } else {
        acc.push(el);
      }

      return acc;
    }, []);

Any help would be much appreciated. Thanks in advance.

4
  • The format of your arrays is invalid,.. (p.s.: Sorry for the edit, I rolled it back , was accidental) Commented Mar 15, 2018 at 16:33
  • By "the same key-value pairs" do you mean specifically id, name, and type, or "everything-but-nodes", or something else altogether? Commented Mar 15, 2018 at 16:37
  • yes. I just edited the question Commented Mar 15, 2018 at 16:39
  • I think you can manage that using a for in/as loop function... something like this pseudo-code: for obj in array for(i=0;i<newArr.length;i++) if(newArr[i].name == obj.name -add them, else -newArr[]=obj Commented Mar 15, 2018 at 16:45

1 Answer 1

2

You could use a Map and collect all items with same id and name

var array = [{ id: "aaa", name: "aaa", type: "director", nodes: [{ type: "manager", id: "111", name: "111" }] }, { id: "aaa", name: "bbb", type: "director", nodes: [{ type: "manager", id: "222", name: "222" }] }, { id: "aaa", name: "aaa", type: "director", nodes: [{ type: "manager", id: "333", name: "333" }] }, { id: "aaa", name: "bbb", type: "director", nodes: [{ type: "manager", id: "444", name: "444" }] }],
    result = [...array.reduce((m, o) => {
        var key = ['id', 'name'].map(k => o[k]).join('|');
        if (m.has(key)) {
            m.get(key).nodes.push(...o.nodes);
        } else {
            m.set(key, Object.assign({}, o, { nodes: o.nodes }));
        }
        return m;
    }, new Map).values()];

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

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

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.