我有一个值重复的数组,如何列出这些值
var result = [ { name: 'Maura Hickman' },
{ name: 'Elena Vega' },
{ name: 'Sylvia Norman' },
{ name: 'Jana Stevenson' },
{ name: 'Colon Reynolds' },
{ name: 'Luisa Rutledge' },
{ name: 'Pearson Marquez' },
{ name: 'Sylvia Norman' },
{ name: 'Jana Stevenson' },
{ name: 'Colon Reynolds' } ]
我尝试这样做
var obj = [ { name: 'Maura Hickman' },
{ name: 'Elena Vega' },
{ name: 'Sylvia Norman' },
{ name: 'Jana Stevenson' },
{ name: 'Colon Reynolds' },
{ name: 'Luisa Rutledge' },
{ name: 'Pearson Marquez' },
{ name: 'Sylvia Norman' },
{ name: 'Jana Stevenson' },
{ name: 'Colon Reynolds' } ];
if (Object.values(obj).indexOf('Jana Stevenson') > -1) {
console.log('has Jana Stevenson');
}
但是没有用。
我期望的是以下
var exist = [{ name: 'Jana Stevenson'} , {name: 'Sylvia Norman' },
{ name: 'Colon Reynolds'}]
答案 0 :(得分:1)
您可以使用filter()
和findIndex()
var arr = [ { name: 'Maura Hickman' },
{ name: 'Elena Vega' },
{ name: 'Sylvia Norman' },
{ name: 'Jana Stevenson' },
{ name: 'Colon Reynolds' },
{ name: 'Luisa Rutledge' },
{ name: 'Pearson Marquez' },
{ name: 'Sylvia Norman' },
{ name: 'Jana Stevenson' },
{ name: 'Colon Reynolds' },
{ name: 'Colon Reynolds' }]
const res = arr.filter((x,i) => arr.findIndex(a => a.name === x.name) !== i)
.filter((x,i,arr) => arr.find(a => a.name === x.name) === x);
console.log(res)
答案 1 :(得分:0)
尝试使用Array#prototype#reduce
,维护一个辅助对象,如果该键尚未在累加器中添加,请将其添加,否则将其推入累加器,最后返回累加器。
result = [{
name: 'Maura Hickman'
},
{
name: 'Elena Vega'
},
{
name: 'Sylvia Norman'
},
{
name: 'Sylvia Norman'
},
{
name: 'Sylvia Norman'
},
{
name: 'Jana Stevenson'
},
{
name: 'Colon Reynolds'
},
{
name: 'Luisa Rutledge'
},
{
name: 'Pearson Marquez'
},
{
name: 'Sylvia Norman'
},
{
name: 'Jana Stevenson'
},
{
name: 'Colon Reynolds'
}
];
// Take the groups which have more than 1 element in it
const duplicateNames = Object.values(result.reduce((acc, curr) => {
// If not existing in accumulator add it
if (!acc[curr.name]) {
acc[curr.name] = [curr];
}
// Else push it simply
else {
acc[curr.name].push(curr);
}
return acc;
}, {}))
.filter(x => x.length > 1)
.map(x => x[0]);
console.log(duplicateNames);
答案 2 :(得分:0)
您可以将Set
用作找到的名称,如果找到该对象,则取该对象;如果找不到,则将该名称添加到集合中并返回false
。
var array = [{ name: 'Maura Hickman' }, { name: 'Elena Vega' }, { name: 'Sylvia Norman' }, { name: 'Jana Stevenson' }, { name: 'Colon Reynolds' }, { name: 'Luisa Rutledge' }, { name: 'Pearson Marquez' }, { name: 'Sylvia Norman' }, { name: 'Jana Stevenson' }, { name: 'Colon Reynolds' }],
found = new Set,
duplicates = array.filter(({ name }) => found.has(name) || !found.add(name));
console.log(duplicates);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 3 :(得分:-1)
例如:
var check = 'Elena Vega';
var result = [
{ name: 'Maura Hickman' },
{ name: 'Elena Vega' },
{ name: 'Sylvia Norman' },
{ name: 'Jana Stevenson' },
{ name: 'Colon Reynolds' },
{ name: 'Luisa Rutledge' },
{ name: 'Pearson Marquez' },
{ name: 'Sylvia Norman' },
{ name: 'Jana Stevenson' },
{ name: 'Colon Reynolds' }
]
var i
for( i = 0; i < result.length; i++ ){
if( result[i].name == check ) {
console.log( 'Found Match: «' + check + '» at result[' + i + '];' );
break;
}
}
P.s。如果您的数组对象仅包含.name
键,则可以仅使用数组,如下所示:
var result = [
'Maura Hickman', // == result[0]
'Elena Vega', // == result[1]
'Sylvia Norman', // == result[2] etc.
'Jana Stevenson',
'Colon Reynolds',
'Luisa Rutledge',
'Pearson Marquez',
'Sylvia Norman',
'Jana Stevenson',
'Colon Reynolds'
]
答案 4 :(得分:-1)
您有一个数组,而不是对象,因此Object.values()仅将返回整个数组。尝试filtering数组。
var obj = [ { name: 'Maura Hickman' },
{ name: 'Elena Vega' },
{ name: 'Sylvia Norman' },
{ name: 'Jana Stevenson' },
{ name: 'Colon Reynolds' },
{ name: 'Luisa Rutledge' },
{ name: 'Pearson Marquez' },
{ name: 'Sylvia Norman' },
{ name: 'Jana Stevenson' },
{ name: 'Colon Reynolds' } ];
var hasJana = obj.filter(itm=>itm.name=='Jana Stevenson').length>0;
if (hasJana) {
console.log('has Jana Stevenson');
}
答案 5 :(得分:-1)
首先,您拥有一个数组,而不是一个对象。事实证明,您不应该调用数组对象,这很令人困惑。从{} !== {}
开始,您需要实际检查要确保唯一的属性。您可以使用以下reducer,然后重新创建对象数组结构。
const res = arr.reduce((a, {name}) => ! a.includes(name) ? a.concat(name) : a, []).map(name => ({name}));
console.log(res)
<script>
const arr = [ { name: 'Maura Hickman' },
{ name: 'Elena Vega' },
{ name: 'Sylvia Norman' },
{ name: 'Jana Stevenson' },
{ name: 'Colon Reynolds' },
{ name: 'Luisa Rutledge' },
{ name: 'Pearson Marquez' },
{ name: 'Sylvia Norman' },
{ name: 'Jana Stevenson' },
{ name: 'Colon Reynolds' } ];
</script>