1

I have the following array of objects. What i need to do is just remove the matching key-val pairs from all the connector arrays.

 [  
       {  
          "connector":[  
             {  
                "name":"CC1"
             },
             {  
                "name":"App1"
             },
             {  
                "name":"CC1"
             },
             {  
                "name":"App2"
             },
             {  
                "name":"CC1"
             },
             {  
                "name":"App3"
             }
          ],
          "connections":[  
             {  
                "source":"CC1",
                "target":"App1"
             },
             {  
                "source":"CC1",
                "target":"App2"
             },
             {  
                "source":"CC1",
                "target":"App3"
             }
          ]
       },
       {  
          "connector":[  
             {  
                "name":"CC1"
             },
             {  
                "name":"App1"
             },
             {  
                "name":"CC1"
             },
             {  
                "name":"App2"
             },
             {  
                "name":"CC1"
             },
             {  
                "name":"App3"
             }
          ],
          "connections":[  
             {  
                "source":"CC1",
                "target":"App1"
             },
             {  
                "source":"CC1",
                "target":"App2"
             },
             {  
                "source":"CC1",
                "target":"App3"
             }
          ]
       },
       {  
          "connector":[  
             {  
                "name":"CC1"
             },
             {  
                "name":"App1"
             },
             {  
                "name":"CC1"
             },
             {  
                "name":"App2"
             },
             {  
                "name":"CC1"
             },
             {  
                "name":"App3"
             }
          ],
          "connections":[  
             {  
                "source":"CC1",
                "target":"App1"
             },
             {  
                "source":"CC1",
                "target":"App2"
             },
             {  
                "source":"CC1",
                "target":"App3"
             }
          ]
       },
       {  
          "connector":[  
             {  
                "name":"CC2"
             },
             {  
                "name":"App2"
             }
          ],
          "connections":[  
             {  
                "source":"CC2",
                "target":"App2"
             }
          ]
       }
    ]

I have tried using a combination of filter, map and the spreadable operator in es6 but have not yet found the best combination that would achieve this. The output that I would want is below:

 [  
           {  
              "connector":[  
                 {  
                    "name":"CC1"
                 },
                 {  
                    "name":"App1"
                 },
                 {  
                    "name":"App2"
                 },
                 {  
                    "name":"App3"
                 }
              ],
              "connections":[  
                 {  
                    "source":"CC1",
                    "target":"App1"
                 },
                 {  
                    "source":"CC1",
                    "target":"App2"
                 },
                 {  
                    "source":"CC1",
                    "target":"App3"
                 }
              ]
           },
           {  
              "connector":[  
                 {  
                    "name":"CC1"
                 },
                 {  
                    "name":"App1"
                 },
                 {  
                    "name":"App2"
                 },
                 {  
                    "name":"App3"
                 }
              ],
              "connections":[  
                 {  
                    "source":"CC1",
                    "target":"App1"
                 },
                 {  
                    "source":"CC1",
                    "target":"App2"
                 },
                 {  
                    "source":"CC1",
                    "target":"App3"
                 }
              ]
           },
           {  
              "connector":[  
                 {  
                    "name":"CC1"
                 },
                 {  
                    "name":"App1"
                 },
                 {  
                    "name":"App2"
                 },
                 {  
                    "name":"App3"
                 }
              ],
              "connections":[  
                 {  
                    "source":"CC1",
                    "target":"App1"
                 },
                 {  
                    "source":"CC1",
                    "target":"App2"
                 },
                 {  
                    "source":"CC1",
                    "target":"App3"
                 }
              ]
           },
           {  
              "connector":[  
                 {  
                    "name":"CC2"
                 },
                 {  
                    "name":"App2"
                 }
              ],
              "connections":[  
                 {  
                    "source":"CC2",
                    "target":"App2"
                 }
              ]
           }
        ]

What would be the most optimal solution to achieve this? thanks in advance for the help..

2
  • Just incase if you are planning to use lodash. This is how you can do that. _.map(data, (i) => { i.connector = _.uniqBy(i.connector, 'name'); return i; }) Commented Oct 17, 2017 at 23:55
  • 1
    The best, most optimal way to do it, would be to stop the redundant data before it even goes into this Array of Objects. Commented Oct 17, 2017 at 23:55

3 Answers 3

1

setTimeout(doit, 100);

function doit() {
  data.forEach(obj => {
    obj.connector = obj.connector.filter(({name}, i, arr) =>
      arr.findIndex(o => o.name === name) === i
    )
  });

  console.log(data);
}

var data = [{
    "connector": [{
        "name": "CC1"
      },
      {
        "name": "App1"
      },
      {
        "name": "CC1"
      },
      {
        "name": "App2"
      },
      {
        "name": "CC1"
      },
      {
        "name": "App3"
      }
    ],
    "connections": [{
        "source": "CC1",
        "target": "App1"
      },
      {
        "source": "CC1",
        "target": "App2"
      },
      {
        "source": "CC1",
        "target": "App3"
      }
    ]
  },
  {
    "connector": [{
        "name": "CC1"
      },
      {
        "name": "App1"
      },
      {
        "name": "CC1"
      },
      {
        "name": "App2"
      },
      {
        "name": "CC1"
      },
      {
        "name": "App3"
      }
    ],
    "connections": [{
        "source": "CC1",
        "target": "App1"
      },
      {
        "source": "CC1",
        "target": "App2"
      },
      {
        "source": "CC1",
        "target": "App3"
      }
    ]
  },
  {
    "connector": [{
        "name": "CC1"
      },
      {
        "name": "App1"
      },
      {
        "name": "CC1"
      },
      {
        "name": "App2"
      },
      {
        "name": "CC1"
      },
      {
        "name": "App3"
      }
    ],
    "connections": [{
        "source": "CC1",
        "target": "App1"
      },
      {
        "source": "CC1",
        "target": "App2"
      },
      {
        "source": "CC1",
        "target": "App3"
      }
    ]
  },
  {
    "connector": [{
        "name": "CC2"
      },
      {
        "name": "App2"
      }
    ],
    "connections": [{
      "source": "CC2",
      "target": "App2"
    }]
  }
];

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

Comments

1

If you are just going to need common operations like map, filter, find and each you don't need lodash:

const data = [{connector:[{name:"CC1"},{name:"App1"},{name:"CC1"},{name:"App2"},{name:"CC1"},{name:"App3"}],connections:[{source:"CC1",target:"App1"},{source:"CC1",target:"App2"},{source:"CC1",target:"App3"}]},{connector:[{name:"CC1"},{name:"App1"},{name:"CC1"},{name:"App2"},{name:"CC1"},{name:"App3"}],connections:[{source:"CC1",target:"App1"},{source:"CC1",target:"App2"},{source:"CC1",target:"App3"}]},{connector:[{name:"CC1"},{name:"App1"},{name:"CC1"},{name:"App2"},{name:"CC1"},{name:"App3"}],connections:[{source:"CC1",target:"App1"},{source:"CC1",target:"App2"},{source:"CC1",target:"App3"}]},{connector:[{name:"CC2"},{name:"App2"}],connections:[{source:"CC2",target:"App2"}]}];

const result = data.map(e => ({ ...e, connector:
  e.connector
    // Filter the original connector array
    // and return only those elements which name is
    // the same as a connection source or target
    .filter(c => e.connections.find(cn => [cn.source, cn.target].indexOf(c.name) !== -1))
        // Remove duplicates by name as @llama notes
    .filter((v, i, ary) => ary.findIndex(c => c.name === v.name) === i)
}));

console.log(result)

4 Comments

You manually removed the duplicates from the input data. If you put them back in, you'll see that your .filter() doesn't remove them.
I think what you mean is that my code does not alter the original data variable right? That is intended.
No, I'm saying that your example alters the original data posted in the question such that the duplicates to remove are no longer there. Put them back in and you'll see that your solution won't remove them. This is because your .find() callback will return true for all duplicate entries.
...looks like you copied the desired output data instead of the actual data. Here's what I mean: jsfiddle.net/v4p8wsbo None of the duplicate objects are being removed.
-1

This will filter out any objects that are repeating, connectors and connections objects can have any number of keys.

mapper = (input) => 
      input.map(x => JSON.stringify(x))          // Stringify
      .filter((x, i, a) => a.indexOf(x) === i)   // Filter out repeating elements
      .map(x => JSON.parse(x))                   // Parse stringified object


output = input.map(item => 
            ({ 
               connector: mapper(item.connector), 
               connections: mapper(item.connections) 
            })
          )

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.