3 回答

TA贡献1818条经验 获得超11个赞
您可以d2对中的每个条目进行分类和比较d1。只需通过提供一个replacer函数作为中的第二个参数来确保在对对象进行字符串化时保持顺序JSON.stringify。
非数组对象的属性不能保证以任何特定顺序进行字符串化。不要依赖于字符串化中同一对象内属性的顺序。
function replacer(obj) {
return Object.keys(obj).sort();
}
var d1 = [{"deviceid":"867874031097770", "simno":"ss343433", "slot":"1"},
{"deviceid":"867874031097770","simno":"ss343433","slot":"1"}];
var d2 = {"deviceid":"867874031097770","slot":"1", "simno":"ss343433"};
function equals(searchArr, objToCheck) {
var allEqual = true;
for (index in searchArr) {
const item = searchArr[index];
if (JSON.stringify(item, replacer(item)) !== JSON.stringify(objToCheck, replacer(objToCheck))) {
(objToCheck)));
allEqual = false;
break;
}
}
return allEqual;
}
if (equals(d1, d2)) {
console.log('All values of properties of d2 match with all entries in d1')
} else {
console.log('d2 values do not match with all entries in d1');
}
添加回答
举报