我有一个数组列表和一个数组。如果productID和attributesData匹配,我想像这样比较这2个数组。返回我在下面给出的列表结构
清单1:
{
"unitPrice": "800.0",
"productTypeTitle": "TYPE",
"productId": "470",
"attributesData": [
{
"attributeName": "COLOR",
"attributeData": "BLUE"
},
{
"attributeName": "SIZE",
"attributeData": "36"
},
{..}
],
"count": 2,
"shopid": "53",
"sessionid": "1643195257593",
...
},
{
},...
]
清单2:
{
"unitPrice": "800.0",
"productTypeTitle": "TYPE",
"productId": "470",
"attributesData": [
{
"attributeName": "SIZE",
"attributeData": "42"
},
{
"attributeName": "COLOR",
"attributeData": "Orange"
},{...}
]
...
}
这里productId是一样的,但attributesData不一样,我怎么能找到呢。我可以检查productId是否相同,但无法比较attributesData。我如何有效地解决的问题
您可以使用lodash isEqual,https://docs-lodash.com/v4/is-equal/
function isEqual(list1,list2):boolean{
return isEqual(list1,list2)
}
如果不使用lodash-然后你需要循环浏览所有属性并比较
function deepEqual(a, b) {
if (a === b) {
return true;
}
if (a == null || typeof(a) != "object" ||
b == null || typeof(b) != "object")
{
return false;
}
var propertiesInA = 0, propertiesInB = 0;
for (var property in a) {
propertiesInA += 1;
}
for (var property in b) {
propertiesInB += 1;
if (!(property in a) || !deepEqual(a[property], b[property])) {
return false;
}
}
return propertiesInA == propertiesInB;
}
此链接源在此处输入链接描述
因为当过程等于时,对象是参考