Home  >  Q&A  >  body text

javascript - js determines whether there are duplicate values ​​in the value in the JSON object?

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

给我你的怀抱给我你的怀抱2687 days ago1127

reply all(4)I'll reply

  • 大家讲道理

    大家讲道理2017-06-12 09:34:44

    if([...new Set(specList.map(item=>item.specDesc))].length < specList.length){
        console.log('有重复')
    }

    reply
    0
  • 阿神

    阿神2017-06-12 09:34:44

    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

    reply
    0
  • PHP中文网

    PHP中文网2017-06-12 09:34:44

    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('不存在');
     }
    }

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-06-12 09:34:44

    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~

    reply
    0
  • Cancelreply