在 JavaScript 对象中查找重复值 [英] Find duplicate value in JavaScript object
本文介绍了在 JavaScript 对象中查找重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定以下对象:
answers = {'a': 3,'b': 2,'c': 3, 'd': 1};
如何查找是否存在重复值?我需要编写一个条件,如果其中两个具有相同的值,则 console.log('duplicate values found')
.
How can I find whether or not there is a duplicate value? I need to write a condition that will say if two of these have the same value, then console.log('duplicate values found')
.
推荐答案
你必须写一个嵌套循环才能找到,
You have to write a nested loop to find that,
var keys = Object.keys(answers);
var dupe = false;
for(var i=0;i<keys.length;i++){
for(var j=i+1;j<keys.length;j++){
if(answers[keys[i]] === answers[keys[j]]){
dupe = true;
break;
}
}
if(dupe){ console.log("dupe value is there.."); break; }
}
这篇关于在 JavaScript 对象中查找重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文