我的收藏品看起来像这样。
var list = [{id:'12345', sequence:null}, {id:'12346', sequence:null}, {id:'12347', sequence:null}, {id:'12348', sequence:1}, {id:'12348', sequence:2}, {id:'12349', sequence:1}, {id:'12349', sequence:1}];
我正在尝试获取一个唯一的列表,以便具有相同id和序列的对象只返回其中一个对象(我们在这里有2个 - {id:'12349',序列:1})
我的代码
var uniqueList = _.uniq(list, function(obj) {
return obj.id && obj.sequence;
});
我们可以像这样使用lodash uniq吗?解决这个问题的方法是什么?
答案 0 :(得分:19)
您可以使用uniqBy()来计算生成列表的标准,其中iteratee回调返回集合中每个项目的id
和sequence
的连接值。当您只希望将特定属性作为唯一性标准时,下面的解决方案非常有用,尤其是当您的集合中的每个对象不仅具有id
和sequence
属性时。
var result = _.uniqBy(list, v => [v.id, v.sequence].join());
var list = [{
id: '12345',
sequence: null
}, {
id: '12346',
sequence: null
}, {
id: '12347',
sequence: null
}, {
id: '12348',
sequence: 1
}, {
id: '12348',
sequence: 2
}, {
id: '12349',
sequence: 1
}, {
id: '12349',
sequence: 1
}, {
id: '123490',
sequence: 1,
extra: 1
}, {
id: '123490',
sequence: 1,
extra: 2
}];
var result = _(list)
.uniqBy(v => [v.id, v.sequence].join())
.value();
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.12.0/lodash.js"></script>
或者,如果您的属性包含要用作条件的非原始值(对象或数组),那么您仍然可以使用上面的解决方案,但是要加入值数组,您必须使用{{3} }
var result = _.uniqBy(list, v => JSON.stringify([v.id, v.sequence]));
var list = [{
id: '12345',
sequence: null
}, {
id: '12346',
sequence: null
}, {
id: '12347',
sequence: null
}, {
id: '12348',
sequence: 1
}, {
id: '12348',
sequence: 2
}, {
id: '12349',
sequence: 1
}, {
id: '12349',
sequence: 1
}, {
id: '123490',
sequence: 1,
extra: 1
}, {
id: '123490',
sequence: 1,
extra: 2
}];
var result = _.uniqBy(list, v => JSON.stringify([v.id, v.sequence]));
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.12.0/lodash.js"></script>
<强>更新强>
对于lodash3解决方案,对于具有原始值的属性,您仍然可以使用JSON.stringify()在第一个解决方案中使用该模式。
var result = _.uniq(list, v => [v.id, v.sequence].join());
var list = [{
id: '12345',
sequence: null
}, {
id: '12346',
sequence: null
}, {
id: '12347',
sequence: null
}, {
id: '12348',
sequence: 1
}, {
id: '12348',
sequence: 2
}, {
id: '12349',
sequence: 1
}, {
id: '12349',
sequence: 1
}, {
id: '123490',
sequence: 1,
extra: 1
}, {
id: '123490',
sequence: 1,
extra: 2
}];
var result = _.uniq(list, v => [v.id, v.sequence].join());
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>
对于非原始属性,您仍然可以使用上面的第二个示例uniq()
var result = _.uniq(list, v => JSON.stringify([v.id, v.sequence]));
var list = [{
id: '12345',
sequence: null
}, {
id: '12346',
sequence: null
}, {
id: '12347',
sequence: null
}, {
id: '12348',
sequence: 1
}, {
id: '12348',
sequence: 2
}, {
id: '12349',
sequence: 1
}, {
id: '12349',
sequence: 1
}, {
id: '123490',
sequence: 1,
extra: 1
}, {
id: '123490',
sequence: 1,
extra: 2
}];
var result = _.uniq(list, v => JSON.stringify([v.id, v.sequence]));
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>
答案 1 :(得分:0)
答案 2 :(得分:0)
您可以使用uniqWith
此方法与_.uniq类似,只是它接受比较器,该比较器被调用以比较数组的元素。使用两个参数调用比较器:(arrVal,othVal)。
在两个值之间执行深度比较,以确定它们是否相同。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:pew="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1.0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1.0"
android:layout_weight=".25">
<ImageView
android:layout_height="wrap_content"
android:layout_weight=".5"
android:layout_width="0dip"
android:src="@drawable/img"
android:scaleType="centerCrop"/>
<RelativeLayout
android:layout_height="match_parent"
android:layout_weight=".5"
android:layout_width="0dip"
android:background="@color/mainBlue">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sezione 1"
android:gravity="left"
android:layout_centerInParent="true"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"
android:layout_margin="10dp"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1.0"
android:layout_weight=".25">
<RelativeLayout
android:layout_height="match_parent"
android:layout_weight=".5"
android:layout_width="0dip"
android:background="@color/mainBlue">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sezione 2"
android:gravity="left"
android:layout_centerInParent="true"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"
android:layout_margin="10dp"/>
</RelativeLayout>
<ImageView
android:layout_height="wrap_content"
android:layout_weight=".5"
android:layout_width="0dip"
android:src="@drawable/img"
android:scaleType="centerCrop"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1.0"
android:layout_weight=".25">
<ImageView
android:layout_height="wrap_content"
android:layout_weight=".5"
android:layout_width="0dip"
android:src="@drawable/img"
android:scaleType="centerCrop"/>
<RelativeLayout
android:layout_height="match_parent"
android:layout_weight=".5"
android:layout_width="0dip"
android:background="@color/mainBlue">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sezione 1"
android:gravity="left"
android:layout_centerInParent="true"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"
android:layout_margin="10dp"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1.0"
android:layout_weight=".25">
<RelativeLayout
android:layout_height="match_parent"
android:layout_weight=".5"
android:layout_width="0dip"
android:background="@color/white">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/icon_news"
android:scaleType="centerInside"
android:layout_centerHorizontal="true"
android:layout_above="@+id/newsTextView"
/>
<TextView
android:id="@+id/newsTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="News"
android:gravity="center_horizontal"
android:layout_centerInParent="true"
android:textColor="@color/mainBlue"
android:textSize="20sp"
android:textStyle="bold"
android:layout_margin="10dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_height="match_parent"
android:layout_weight=".5"
android:layout_width="0dip"
android:background="@color/white">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/icon_contacts"
android:scaleType="centerInside"
android:layout_centerHorizontal="true"
android:layout_above="@+id/contactTextView"
/>
<TextView
android:id="@+id/contactTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contatti"
android:gravity="center_horizontal"
android:layout_centerInParent="true"
android:textColor="@color/mainBlue"
android:textSize="20sp"
android:textStyle="bold"
android:layout_margin="10dp"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>