javascript - js determines whether there are duplicate values ​​in the value in the JSON object?
给我你的怀抱
给我你的怀抱 2017-06-12 09:32:44
0
4
1189

How to determine if the value of specDesc in the JSON object cannot be the same

给我你的怀抱
给我你的怀抱

reply all(4)
大家讲道理
if([...new Set(specList.map(item=>item.specDesc))].length < specList.length){
    console.log('有重复')
}
阿神

You can first traverse and push the value of obj.specDesc into an array, and then write a function to determine whether there are duplicates in the array

洪涛
var obj={};
for(var i=0,l=specList.length;i<l;i++){
 if(obj[specDesc[i].specDesc]){
   console.log('已存在');
 }else{
   obj[specDesc[i].specDesc]=specDesc[i].specDesc;
   console.log('不存在');
 }
}
仅有的幸福

Can’t we just judge directly? a['spec'] == b['spec'], If you want to compare the values ​​corresponding to all keys, you need to traverse all the keys of one of them and compare them to find out whether the corresponding values ​​of the other keys are equal.

Since the questioner only sent a screenshot, it is difficult to study the meaning of the question. My understanding: Two objects a and b are not allowed to have a key-value pair that is the same

Then the solution is as follows:

var a = {age:1, spec:'hello'},
    b = {age:21, spec:'hello'};
    
function noRepeat(obj1,obj2){
    var res = false;
    for(var key in obj1){
        if(obj1[key]==obj2[key]){
            res = true;
            break;
        }
    }
    return res;
}

noRepeat(a, b); // true

This is similar to the truth. How can the subject of the question answer this question? If you have any additional questions, please add them~

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template