I need to replace two (nested) forEach loops with a some function. The purpose of the code block is to check if the [1] elements in arrayOne are within the max & min of arrayTwo.
The code currently works fine and looks like this:
JavaScript
x
9
1
var result = false;
2
arrayOne.forEach((arrayOneElement) => {
3
arrayTwo.forEach((arrayTwoElement) => {
4
if (arrayOneElement[1] > arrayTwoElement[1].max || arrayOneElement[1] < arrayTwoElement[1].min) {
5
result = true;
6
}
7
});
8
});
9
Let me know if it isn’t clear enough. Really appreciate your help.
Advertisement
Answer
Yes, you can use Array#some
in this situation. It would be done like this
JavaScript
1
6
1
const result = arrayOne.some((arrayOneElement) => {
2
return arrayTwo.some((arrayTwoElement) => {
3
return arrayOneElement[1] > arrayTwoElement[1].max || arrayOneElement[1] < arrayTwoElement[1].min
4
});
5
});
6