The simple answer is putting a return
statement inside of the forEach
loop will do the work for you as @nnnnnn
said,
elementsCollection.forEach(function(element){
if (!element.shouldBeProcessed)
return; // stop processing this iteration
// This part will be avoided if not neccessary
doSomeLengthyOperation();
});
but if you want a deep answer to that question then just be with me.
Assuming that you don't know the implementation of forEach
loop then take a look at the following implementation of forEach
loop which is exactly the one specified in ECMA-262, 5th edition for forEach
loop.
Source Array.prototype.forEach() - JavaScript | MDN
if (!Array.prototype['forEach']) {
Array.prototype.forEach = function(callback, thisArg) {
if (this == null) { throw new TypeError('Array.prototype.forEach called on null or undefined'); }
var T, k;
// 1. Let O be the result of calling toObject() passing the
// |this| value as the argument.
var O = Object(this);
// 2. Let lenValue be the result of calling the Get() internal
// method of O with the argument "length".
// 3. Let len be toUint32(lenValue).
var len = O.length >>> 0;
// 4. If isCallable(callback) is false, throw a TypeError exception.
// See: https://es5.github.com/#x9.11
if (typeof callback !== "function") { throw new TypeError(callback + ' is not a function'); }
// 5. If thisArg was supplied, let T be thisArg; else let
// T be undefined.
if (arguments.length > 1) { T = thisArg; }
// 6. Let k be 0
k = 0;
// 7. Repeat, while k < len
while (k < len) {
var kValue;
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the HasProperty
// internal method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
if (k in O) {
// i. Let kValue be the result of calling the Get internal
// method of O with argument Pk.
kValue = O[k];
// ii. Call the Call internal method of callback with T as
// the this value and argument list containing kValue, k, and O.
callback.call(T, kValue, k, O);
}
// d. Increase k by 1.
k++;
}
// 8. return undefined
};
}
You really don't need to understand every line of the above code because what we are interested in is the while
loop,
while (k < len) {
var kValue;
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the HasProperty
// internal method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
if (k in O) {
// i. Let kValue be the result of calling the Get internal
// method of O with argument Pk.
kValue = O[k];
// ii. Call the Call internal method of callback with T as
// the this value and argument list containing kValue, k, and O.
callback.call(T, kValue, k, O);
}
// d. Increase k by 1.
k++;
}
If you notice then there is a statement callback.call(T, KValue, K, O)
again we are not interested in the arguments given to the call()
method here but what we are really interested in is the callback
binding which is a function
that you give to your forEach
loop in javascript. See the call
method just calls the object (javascript function) it is called on with a this
value and arguments provided individually.
If you don't understand what call is then take a look at Function.prototype.Call() - JavaScript | MDN.
Just think about this if at any point your function that is callback
in this case returns at any point the loop will be updated as usual. The loop doesn't care about if the callback
function has performed each and every step given to it or not if the control has returned to the loop the loop has to do its job. Every time the loop is updated the callback
is called with new set of values as you can see there T, KValue, K, O
are changing every time the loop updates, so if at any point you return from your function i.e., callback
you are just handing the control to the loop you are called in no matter at what point you return from your function, if you want to skip some operations inside of your function at a given condition then just put the return statement before those statements you want to skip.
That is how you skip a iteration inside of a forEach
loop.
for(let element of data) { if(element.childData == "") { continue; } }
Best solution