Kevin Powell - CommunityKP-C
Kevin Powell - Community3y ago
370 replies
Matt

Loop through Two Arrays (JS)

I have two arrays which I'm trying to loop through to determine if they have matching values. Here is a portion of the code:

currentProducts.forEach(item1 => {
                pastProducts.forEach(item2 => {

                    console.log("Item 1: " + item1.id + " " + "Item 2: " + item2)

                    if (item1.id === item2) {
                        console.log("match")
                    } else {
                        console.log("no match")
                    }
                })
            });


When I run the comparison, it returns no match, despite there being a match (see screenshot). What am I doing wrong here?
image.png
Was this page helpful?