gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:15:10 25 4
gpt4 key购买 nike

我有以下对象数组。我需要做的只是从所有连接器数组中删除匹配的键值对。

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

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

最佳答案

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"
}]
}
];

关于javascript - 从对象数组中删除重复元素 - es6?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46800721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com