0

我有以下對象數組。我需要做的只是從所有連接器陣列中刪除匹配的鍵值對。從對象數組中刪除重複的元素 - es6?

[ 
     { 
      "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可塗抹運營商的合併審理,但還沒有發現,將實現這一目標的最佳組合。 ,我想的輸出如下:

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

什麼是最優化的解決方案來實現這一目標?在此先感謝您的幫助..

+0

只要你打算使用lodash。這是你如何做到的。 (i.connector = _.uniqBy(i.connector,'name'); return i; })' –

+0

這是最好的,最理想的方式來做到這一點,將會在冗餘數據進入這個對象數組之前停止。 – PHPglue

回答

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

如果你只是要需要像地圖,過濾器常用的操作,發現每一次你不需要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)

+0

您手動刪除輸入「數據」中的重複項。如果你把它們放回去,你會看到你的'.filter()'不會刪除它們。 – llama

+0

我想你的意思是說我的代碼不會改變原來的'data'變量吧?這是打算。 – nicooga

+0

不,我要說的是,您的示例會更改發佈在問題中的原始數據,以便重複刪除的內容不再存在。把它們放回去,你會發現你的解決方案不會刪除它們。這是因爲你的'.find()'回調函數將爲所有重複條目返回「true」。 – llama

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