[英]Removing duplicates from two arrays in javascript based on a single attribute
I have two arrays in javascript, where i want to do two different operations我在 javascript 中有两个 arrays,我想做两个不同的操作
I have this function so far到目前为止我有这个 function
export function generateDisplayedLabels(systemLabels, masterState){
const mappedSystemlabels = systemLabels.map(label => Object.assign(label, {type: "system"}))
const mappedMasterlabels = masterState.map(label => Object.assign(label, {type: "master"}))
const displayedLabels = _.union(mappedSystemlabels, mappedMasterState);
return displayedLabels
}
This would work except for the fact, that whenever i map over the objects in the beginning, the "unique" elements are no longer unique, because they have another attribute mapped upon it.这将起作用,除了以下事实,即每当我在开始时对对象进行 map 时,“唯一”元素不再是唯一的,因为它们有另一个属性映射在其上。 Is there a time efficient way, that i can filter out the none unique elements, ignoring the attribute, that have been mapped onto it.
是否有一种省时的方法,我可以过滤掉已映射到其上的非唯一元素,而忽略属性。
let ar1 = [ { id: 1, name: 'stack', }, { id: 2, name: 'react', }, ]; let ar2 = [ { id: 1, name: 'javascript', }, { id: 2, name: 'overflow', }, { id: 2, name: 'react', }, ]; console.log(_.unionBy(ar1, ar2, 'id')); console.log("======"); console.log(_.unionBy(ar1, ar2, 'name'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.