Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
114
rated 0 times [  116] [ 2]  / answers: 1 / hits: 16568  / 7 Years ago, tue, july 11, 2017, 12:00:00

I am having an object like below.i will loop through the array of objects and check for any object property has value.the following code works very fine, it sets a flag to true,if any of the objects has an empty value,but it doesn't break out of the obj.forEach loop it just exists out of the immediate parent for loop only.how to exit out of the obj.forEach loop if any of the object property has value





var obj = [
{
fname: name1,
lname:
},
{
fname: name2,
lname: lname2
},
{
fname: ,
lname: lname3
}
];
var hasEmptyValue = false
var hasEmptyProperty = obj3.forEach(function(item) {
for (var key in item) {
if (item.hasOwnProperty(key) && item[key] == ) {
saveIt = true;
break;
}
console.log(key->,key,value ->,item[key]);
}
});




More From » javascript

 Answers
7

According to the MDN web docs:




There is no way to stop or break a forEach() loop other than by
throwing an exception.




Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach


[#57129] Saturday, July 8, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marib

Total Points: 596
Total Questions: 120
Total Answers: 95

Location: Nauru
Member since Thu, Feb 2, 2023
1 Year ago
;