How to determine if the value of specDesc in the JSON object cannot be the same
if([...new Set(specList.map(item=>item.specDesc))].length < specList.length){
console.log('有重復(fù)')
}
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~