How to break out of foreach loop in javascript

I am new to Javascrript. I have a variable that has the following data:

var result = false; [{"a": "1","b": null},{"a": "2","b": 5}].forEach(function(call){ console.log(call); var a = call['a']; var b = call['b']; if(a == null || b == null){ result = false break; } }); 

I want to break the loop if the key has a NULL value. How can i do this?

+6
source share
1 answer

Do not use .forEach and then for the loop:

 var myObj = [{"a": "1","b": null},{"a": "2","b": 5}] var result = false for(var call of myObj) { console.log(call) var a = call['a'], b = call['b'] if(a == null || b == null) { result = false break } } 
+29
source

All Articles