从对象数组中删除重复的元素 - es6?

时间:2017-10-17 23:35:57

标签: javascript arrays ecmascript-6 duplicates javascript-objects

我有以下对象数组。我需要做的就是从所有连接器阵列中删除匹配的key-val对。

 [  
       {  
          "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"
             }
          ]
       }
    ]

我尝试在es6中使用filter,map和spreadable运算符的组合,但尚未找到实现此目的的最佳组合。 我想要的输出如下:

 [  
           {  
              "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"
                 }
              ]
           }
        ]

实现这一目标的最佳解决方案是什么?在此先感谢您的帮助..

3 个答案:

答案 0 :(得分: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"
    }]
  }
];

答案 1 :(得分:1)

如果您只需要普通的操作,例如map,filter,find,那么您不需要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)

答案 2 :(得分:-1)

这将过滤掉任何重复的对象,connectorsconnections个对象可以包含任意数量的键。

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) 
            })
          )