Is there anyone who can help me with this task? I am attempting to compare two separate arrays and push values when the comparison is equal. The arrays in question are imageslide (including therapy) and totalValues. My goal is to compare names like cats and dogs, and if a condition is met, I want to push their respective image URLs.
var imageslide = {
"therapy": [
{
"name": "cats",
"images": [
{ "url": "cat/firstimg.jpg" },
{ "url": "cat/secondimg.jpg" },
{ "url": "cat/thirdimg.jpg" },
{ "url": "cat/fourthimg.jpg" }
]
},
{
"name": "dogs",
"images": [
{ "url": "dog/firstdog.jpeg" },
{ "url": "dog/seconddog.jpg" },
{ "url": "dog/thirddog.jpg" },
{ "url": "dog/fourthdog.jpg" }
]
},
]
}
var totalValues = ["cats", "dogs"];
I have attempted the following approach:
var imageArray = imageslide.therapy
function compare(imageArray, totalValues ){
imageArray.forEach((e1) => totalValues.forEach((e2) => {
if(e1.name == e2){
console.log(e1.name, ",", e2)
}
})
}