I have an array of objects and I want to get a new array from it that is unique based only on a single property, is there a simple way to achieve this?
我有一个对象数组,我想从它获得一个新的数组,它只是基于一个属性是唯一的,有一个简单的方法来实现这一点吗?
Eg.
[ { id: 1, name: 'bob' }, { id: 1, name: 'bill' }, { id: 1, name: 'bill' } ]
Would result in 2 objects with name = bill removed once.
将导致名称= bill的2个对象被删除一次。
6 个解决方案
#1
55
Use the uniq function
使用uniq功能
var destArray = _.uniq(sourceArray, function(x){
return x.name;
});
From the docs:
来自文档:
Produces a duplicate-free version of the array, using === to test object equality. If you know in advance that the array is sorted, passing true for isSorted will run a much faster algorithm. If you want to compute unique items based on a transformation, pass an iterator function.
生成数组的无副本版本,使用===来测试对象相等性。如果您事先知道数组已排序,则为isSorted传递true将运行更快的算法。如果要基于转换计算唯一项,请传递迭代器函数。
In the above example, the function uses the objects name in order to determine uniqueness.
在上面的示例中,函数使用对象名称来确定唯一性。
#2
8
If you prefer to do things yourself without Lodash, and without getting verbose, try this uniq filter with optional uniq by property:
如果您喜欢在没有Lodash的情况下自己做事,并且没有得到详细信息,请尝试使用可选的uniq by属性的uniq过滤器:
const uniqFilterAccordingToProp = function (prop) {
if (prop)
return (ele, i, arr) => arr.map(ele => ele[prop]).indexOf(ele[prop]) === i
else
return (ele, i, arr) => arr.indexOf(ele) === i
}
Then, use it like this:
然后,像这样使用它:
const obj = [ { id: 1, name: 'bob' }, { id: 1, name: 'bill' }, { id: 1, name: 'bill' } ]
obj.filter(uniqFilterAccordingToProp('abc'))
Or for plain arrays, just omit the parameter, while remembering to invoke:
或者对于普通数组,只需省略参数,同时记住调用:
[1,1,2].filter(uniqFilterAccordingToProp())
#3
6
If you want to check all the properties then lodash 4 comes with _.uniqWith(sourceArray, _.isEqual)
如果你想检查所有的属性,那么lodash 4附带_.uniqWith(sourceArray,_.isEqual)
#4
2
You can use the _.uniqBy function
您可以使用_.uniqBy函数
var array = [ { id: 1, name: 'bob' }, { id: 2, name: 'bill' }, { id: 1, name: 'bill' },{ id: 2, name: 'bill' } ];
var filteredArray = _.uniqBy(array,function(x){ return x.id && x.name;});
console.log(filteredArray)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.js"></script>
In the above example, filtering is based on the uniqueness of combination of properties id & name.
在上面的示例中,过滤基于属性id和名称组合的唯一性。
if you have multiple properties for an object. then to find unique array of objects based on specific properties, you could follow this method of combining properties inside _.uniqBy() method.
如果您有一个对象的多个属性。然后根据特定属性找到唯一的对象数组,您可以按照这种方法在_.uniqBy()方法中组合属性。
#5
2
A better and quick approach
一种更好,更快捷的方法
var table = [
{
a:1,
b:2
},
{
a:2,
b:3
},
{
a:1,
b:4
}
];
let result = [...new Set(table.map(item => item.a))];
document.write(JSON.stringify(result));
#6
1
In case you need pure JavaScript solution:
如果您需要纯JavaScript解决方案:
var uniqueProperties = {};
var notUniqueArray = [ { id: 1, name: 'bob' }, { id: 1, name: 'bill' }, { id: 1, name: 'bill' } ];
for(var object in notUniqueArray){
uniqueProperties[notUniqueArray[object]['name']] = notUniqueArray[object]['id'];
}
var uniqiueArray = [];
for(var uniqueName in uniqueProperties){
uniqiueArray.push(
{id:uniqueProperties[uniqueName],name:uniqueName});
}
//uniqiueArray
#1
55
Use the uniq function
使用uniq功能
var destArray = _.uniq(sourceArray, function(x){
return x.name;
});
From the docs:
来自文档:
Produces a duplicate-free version of the array, using === to test object equality. If you know in advance that the array is sorted, passing true for isSorted will run a much faster algorithm. If you want to compute unique items based on a transformation, pass an iterator function.
生成数组的无副本版本,使用===来测试对象相等性。如果您事先知道数组已排序,则为isSorted传递true将运行更快的算法。如果要基于转换计算唯一项,请传递迭代器函数。
In the above example, the function uses the objects name in order to determine uniqueness.
在上面的示例中,函数使用对象名称来确定唯一性。
#2
8
If you prefer to do things yourself without Lodash, and without getting verbose, try this uniq filter with optional uniq by property:
如果您喜欢在没有Lodash的情况下自己做事,并且没有得到详细信息,请尝试使用可选的uniq by属性的uniq过滤器:
const uniqFilterAccordingToProp = function (prop) {
if (prop)
return (ele, i, arr) => arr.map(ele => ele[prop]).indexOf(ele[prop]) === i
else
return (ele, i, arr) => arr.indexOf(ele) === i
}
Then, use it like this:
然后,像这样使用它:
const obj = [ { id: 1, name: 'bob' }, { id: 1, name: 'bill' }, { id: 1, name: 'bill' } ]
obj.filter(uniqFilterAccordingToProp('abc'))
Or for plain arrays, just omit the parameter, while remembering to invoke:
或者对于普通数组,只需省略参数,同时记住调用:
[1,1,2].filter(uniqFilterAccordingToProp())
#3
6
If you want to check all the properties then lodash 4 comes with _.uniqWith(sourceArray, _.isEqual)
如果你想检查所有的属性,那么lodash 4附带_.uniqWith(sourceArray,_.isEqual)
#4
2
You can use the _.uniqBy function
您可以使用_.uniqBy函数
var array = [ { id: 1, name: 'bob' }, { id: 2, name: 'bill' }, { id: 1, name: 'bill' },{ id: 2, name: 'bill' } ];
var filteredArray = _.uniqBy(array,function(x){ return x.id && x.name;});
console.log(filteredArray)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.js"></script>
In the above example, filtering is based on the uniqueness of combination of properties id & name.
在上面的示例中,过滤基于属性id和名称组合的唯一性。
if you have multiple properties for an object. then to find unique array of objects based on specific properties, you could follow this method of combining properties inside _.uniqBy() method.
如果您有一个对象的多个属性。然后根据特定属性找到唯一的对象数组,您可以按照这种方法在_.uniqBy()方法中组合属性。
#5
2
A better and quick approach
一种更好,更快捷的方法
var table = [
{
a:1,
b:2
},
{
a:2,
b:3
},
{
a:1,
b:4
}
];
let result = [...new Set(table.map(item => item.a))];
document.write(JSON.stringify(result));
#6
1
In case you need pure JavaScript solution:
如果您需要纯JavaScript解决方案:
var uniqueProperties = {};
var notUniqueArray = [ { id: 1, name: 'bob' }, { id: 1, name: 'bill' }, { id: 1, name: 'bill' } ];
for(var object in notUniqueArray){
uniqueProperties[notUniqueArray[object]['name']] = notUniqueArray[object]['id'];
}
var uniqiueArray = [];
for(var uniqueName in uniqueProperties){
uniqiueArray.push(
{id:uniqueProperties[uniqueName],name:uniqueName});
}
//uniqiueArray