
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
break-foreach
Advanced tools
This is a method that allows you to stop the iteration of forEach function in ES6+.
Let's Break the forEach Function! 😎
This is a method that allows you to stop the iteration of forEach function in ES6+. No longer need to stop the forEach with a ugly syntax 'try-catch'.
npm i break-foreach
Or copy the JavaScript code on https://github.com/xun19/break-foreach
const wrapForEach = require('break-foreach')
const array = [1, 2, -1, 3, 4]
wrapForEach({ target: array })
array.forEach((val, index, arr) => {
if (val < 0) return false // Iteration will stop because you return a Break Flag.
console.log(val, index, arr)
})
// output:
// 1, 0, [1, 2, -1, 3, 4]
// 2, 1, [1, 2, -1, 3, 4]
// Iteration stops on this step.
const wrapForEach = require('break-foreach')
/* Array */
const array = [1, 2, -1, 3, 4]
wrapForEach({ target: array })
array.forEach((val, index, arr) => {
if (val < 0) return false // Iteration will stop because you return a Break Flag.
console.log(val, index, arr)
})
// output:
// 1, 0, [1, 2, -1, 3, 4]
// 2, 1, [1, 2, -1, 3, 4]
// Iteration stops on this step.
/* Set */
const set = new Set([1, 2, -1, 3])
wrapForEach({ target: set })
set.forEach((val, index, s) => {
if (val < 0) return false // Iteration will stop because you return a Break Flag.
console.log(val, index, s)
})
// output:
// 1, 0, Set([1, 2, -1, 3])
// 2, 1, Set([1, 2, -1, 3])
// Iteration stops on this step.
/* Map */
const map = new Map([[0, 0], [{}, 1], ['a', -1], ['b', -2]])
wrapForEach({ target: map })
map.forEach((val, key, m) => {
if (val < 0) return false // Iteration will stop because you return a Break Flag.
console.log(val, key, m)
})
// output:
// 1, 0, Map([[0, 0], [{}, 1], ['a', -1], ['b', -2]])
// 2, {}, Map([[0, 0], [{}, 1], ['a', -1], ['b', -2]])
// Iteration stops on this step.
/* Object */
const obj = {a: 1, b: 2, c: -1, d: 3}
wrapForEach({ target: obj })
obj.forEach((val, key, o) => {
if (val < 0) return false // Iteration will stop because you return a Break Flag.
console.log(val, key, o)
})
// output:
// 1, 'a', {a: 1, b: 2, c: -1, d: 3}
// 2, 'b', {a: 1, b: 2, c: -1, d: 3}
// Iteration stops on this step.
const wrapForEach = require('break-foreach')
const array = [1, 2, -1, 3, 4]
const BREAK = 'my break flag' // It can be any Type.
wrapForEach({
target: array,
flag: BREAK
})
array.myForEach((val, index, arr) => {
if (val < 0) return BREAK // return your Break Flag here.
console.log(val, index, arr)
})
// output:
// 1, 0, [1, 2, -1, 3, 4]
// 2, 1, [1, 2, -1, 3, 4]
// Iteration stops on this step.
const wrapForEach = require('break-foreach')
const array = [1, 2, -1, 3, 4]
wrapForEach({
target: array,
funcName: 'myForEach'
})
array.myForEach((val, index, arr) => {
if (val < 0) return false // Iteration will stop because you return a Break Flag.
console.log(val, index, arr)
})
// output:
// 1, 0, [1, 2, -1, 3, 4]
// 2, 1, [1, 2, -1, 3, 4]
// Iteration stops on this step.
array.forEach === Array.prototype.forEach // true. Native forEach function will be remained
https://github.com/xun19/break-foreach
If you think this component has brought you help, welcome to star and provide valuable advice ~ 😊
来把forEach函数break掉吧! 😎
这个模块能让你停止一个forEach函数的遍历,适用于ES6+。你可以不再用“try-catch”的写法去实现这个功能。
npm i break-foreach
Or copy the JavaScript code on https://github.com/xun19/break-foreach
const wrapForEach = require('break-foreach')
const array = [1, 2, -1, 3, 4]
wrapForEach({ target: array })
array.forEach((val, index, arr) => {
if (val < 0) return false // 返回了一个flag,遍历将会停止
console.log(val, index, arr)
})
// output:
// 1, 0, [1, 2, -1, 3, 4]
// 2, 1, [1, 2, -1, 3, 4]
// 遍历会在这一步停止
const wrapForEach = require('break-foreach')
/* Array */
const array = [1, 2, -1, 3, 4]
wrapForEach({ target: array })
array.forEach((val, index, arr) => {
if (val < 0) return false // 返回了一个flag,遍历将会停止
console.log(val, index, arr)
})
// output:
// 1, 0, [1, 2, -1, 3, 4]
// 2, 1, [1, 2, -1, 3, 4]
// 遍历会在这一步停止
/* Set */
const set = new Set([1, 2, -1, 3])
wrapForEach({ target: set })
set.forEach((val, index, s) => {
if (val < 0) return false // 返回了一个flag,遍历将会停止
console.log(val, index, s)
})
// output:
// 1, 0, Set([1, 2, -1, 3])
// 2, 1, Set([1, 2, -1, 3])
// 遍历会在这一步停止
/* Map */
const map = new Map([[0, 0], [{}, 1], ['a', -1], ['b', -2]])
wrapForEach({ target: map })
map.forEach((val, key, m) => {
if (val < 0) return false // 返回了一个flag,遍历将会停止
console.log(val, key, m)
})
// output:
// 1, 0, Map([[0, 0], [{}, 1], ['a', -1], ['b', -2]])
// 2, {}, Map([[0, 0], [{}, 1], ['a', -1], ['b', -2]])
// 遍历会在这一步停止
/* Object */
const obj = {a: 1, b: 2, c: -1, d: 3}
wrapForEach({ target: obj })
obj.forEach((val, key, o) => {
if (val < 0) return false // 返回了一个flag,遍历将会停止
console.log(val, key, o)
})
// output:
// 1, 'a', {a: 1, b: 2, c: -1, d: 3}
// 2, 'b', {a: 1, b: 2, c: -1, d: 3}
// 遍历会在这一步停止
const wrapForEach = require('break-foreach')
const array = [1, 2, -1, 3, 4]
const BREAK = 'my break flag' // flag可以是任意类型
wrapForEach({
target: array,
flag: BREAK
})
array.myForEach((val, index, arr) => {
if (val < 0) return BREAK // 在这里返回你的flag
console.log(val, index, arr)
})
// output:
// 1, 0, [1, 2, -1, 3, 4]
// 2, 1, [1, 2, -1, 3, 4]
// 遍历会在这一步停止
const wrapForEach = require('break-foreach')
const array = [1, 2, -1, 3, 4]
wrapForEach({
target: array,
funcName: 'myForEach'
})
array.myForEach((val, index, arr) => {
if (val < 0) return false // 返回了一个flag,遍历将会停止
console.log(val, index, arr)
})
// output:
// 1, 0, [1, 2, -1, 3, 4]
// 2, 1, [1, 2, -1, 3, 4]
// 遍历会在这一步停止
array.forEach === Array.prototype.forEach // true. 原生的forEach函数将会继续保留
https://github.com/xun19/break-foreach
If you think this component has brought you help, welcome to star and provide valuable advice ~ 😊
FAQs
This is a method that allows you to stop the iteration of forEach function in ES6+.
We found that break-foreach demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.