Skip to content
Advertisement

How do I return 2 values from two arrays when using filter

I have a Javascript code that uses forEach to loop through each array to check if it fulfils a condition. Then, it calls for another function. Note: by looping through every element in arr1 and arr2, I’m checking if any elements in arr1 are colliding with any elements in arr2. isCollision is a function which calculates their distance if they intersect

JavaScript

Right now, I do not want it to return undefined. So I was wondering is there a way to get the x & y values so that I can map it to another function. I tried this

JavaScript

But right now it’s only returning the value of y. Is there a way for it to return both x and y?

Advertisement

Answer

Try this:

JavaScript

First map to {x:y} and then remove not found items.

To cast to a non-undefined type, use a another map.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement