“JavaScript Break Foreach” Kode Jawaban

JavaScript Break Foreach

//break out of for loop
for (i = 0; i < 10; i++) {
    if (i === 3) { break; }
}
Grepper

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;
}
Silly Shrew

JavaScript sebelum setiap contoh istirahat


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;
Sparkling Stoat

Jawaban yang mirip dengan “JavaScript Break Foreach”

Pertanyaan yang mirip dengan “JavaScript Break Foreach”

Lebih banyak jawaban terkait untuk “JavaScript Break Foreach” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya