如何检查对象数组是否具有重复的属性值? [英] How can I check if the array of objects have duplicate property values?
本文介绍了如何检查对象数组是否具有重复的属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一些帮助来迭代数组,我总是卡住或重新发明轮子.
I need some help with iterating through array, I keep getting stuck or reinventing the wheel.
values = [
{ name: 'someName1' },
{ name: 'someName2' },
{ name: 'someName1' },
{ name: 'someName1' }
]
如何检查数组中是否有两个(或多个)相同的名称值?我不需要计数器,如果数组值不唯一,只需设置一些变量.请记住,数组长度是动态的,数组值也是如此.
How could I check if there are two (or more) same name value in array? I do not need a counter, just setting some variable if array values are not unique. Have in mind that array length is dynamic, also array values.
推荐答案
使用 array.prototype.map 和 array.prototype.some:
var values = [
{ name: 'someName1' },
{ name: 'someName2' },
{ name: 'someName4' },
{ name: 'someName2' }
];
var valueArr = values.map(function(item){ return item.name });
var isDuplicate = valueArr.some(function(item, idx){
return valueArr.indexOf(item) != idx
});
console.log(isDuplicate);
这篇关于如何检查对象数组是否具有重复的属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文