0

i have a list of array and a single array. I want to compare this 2 array like this if productID and attributesData match return something my list structure given below

List1:

    {
        "unitPrice": "800.0",
        "productTypeTitle": "TYPE",
       
        "productId": "470",
        
        "attributesData": [
            {
                "attributeName": "COLOR",
                "attributeData": "BLUE"
            },
            {
                "attributeName": "SIZE",
                "attributeData": "36"
            },
           {..}
        ],
        "count": 2,
        "shopid": "53",
        "sessionid": "1643195257593",
...
    },
    {
        },...
]

List2:

{
    "unitPrice": "800.0",
    "productTypeTitle": "TYPE",
    "productId": "470",
    "attributesData": [
        {
            "attributeName": "SIZE",
            "attributeData": "42"
        },
        {
            "attributeName": "COLOR",
            "attributeData": "Orange"
        },{...}
    ]
...
}

Here productId is same but attributesData not same how can I find out that.I am able check if productId is same or not but unable to compare attributesData .How I can solve this problem of efficient way

1 Answer 1

1

You can use lodash isEqual, https://docs-lodash.com/v4/is-equal/

function isEqual(list1,list2):boolean{
return isEqual(list1,list2)
}

if not using lodash -then you need to cycle through all the properties and compare

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;
}

this link source enter link description here

because object is reference when process equaling

Sign up to request clarification or add additional context in comments.

1 Comment

this will not work for me your function comparing whole list i need to compare based on productID and attributesData

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.