Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

foreach break js

var BreakException = {};

try {
  [1, 2, 3].forEach(function(el) {
    console.log(el);
    if (el === 2) throw BreakException;
  });
} catch (e) {
  if (e !== BreakException) throw e;
}
Comment

javascript fore each break example


The for…of loop would be the preferred solution to this problem. It provides clean easy to read syntax and also lets us use break once again. 

let data = [
    {name: 'Rick'},{name: 'Steph'},{name: 'Bob'}
  ]

for (let obj of data) {
  console.log(obj.name)
  if (obj.name === 'Steph') break;
Comment

javascript foreach break

//Use some instead
[1, 2, 3].some(function(el) {
  console.log(el);
  return el === 2;
});
Comment

can i use break in foreach javascript

/* use for...of instead 
Officially, there is no proper way to break out of a forEach loop in javascript.
Using the familiar break syntax will throw an error
If breaking the loop is something you really need, 
it would be best to consider using a traditional loop. */
Comment

PREVIOUS NEXT
Code Example
Javascript :: tab adds tab textarea javascript 
Javascript :: how to install vue 
Javascript :: encode jwt token javascript 
Javascript :: JavaScript Create Multiple Objects with Constructor Function 
Javascript :: click a link with javascript 
Javascript :: javascript object to array 
Javascript :: Downward Triangle Star Pattern in JavaScript 
Javascript :: javascript compare values of two arrays 
Javascript :: node app 
Javascript :: js read xml file 
Javascript :: email regular expression javascript 
Javascript :: settimeout in loop javascript 
Javascript :: javascript document.createElement add function 
Javascript :: span change jquery 
Javascript :: javascript code to open excel file and read contents 
Javascript :: preloader js code 
Javascript :: error metro bundler process exited with code 1 react native 
Javascript :: javascript change background color setinterval 
Javascript :: javascript is null 
Javascript :: how to import dotenv in react 
Javascript :: queryselectorall javascript images in list 
Javascript :: discord token 
Javascript :: datatables get all checkboxes with pagination 
Javascript :: String.toLower() js 
Javascript :: react blur background 
Javascript :: sanitizer content nodejs 
Javascript :: react usereducer 
Javascript :: Next js window is not defined solution 
Javascript :: split array into chunks javascript 
Javascript :: repeat an array multiple times in js 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =