The cleaner way would be to not use .forEach
. It’s almost never needed if you’re using TypeScript or a modern version of JavaScript:
example() { for (let item of this.items) { if (item === 3) { return; } } // Do stuff in case forEach has not returned }
If the code inside your loop doesn’t have any side-effects and you’re just checking for a condition on each item, you could also use a functional approach with .some
:
example() { if (this.items.some(item => item === 3)) { return; } // Do stuff in case we have not returned }
Related Posts:
- What is TypeScript and why would I use it in place of JavaScript? [closed]
- ES6 Map in Typescript
- Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’
- How to define type for a function callback (as any function type, not universal any) used in a method parameter
- TypeScript React.FC
confusion - How to perform string interpolation in TypeScript?
- Angular: mat-form-field must contain a MatFormFieldControl
- (change) vs (ngModelChange) in angular
- Typescript : Property does not exist on type ‘object’
- Angular ForEach in Angular4/Typescript?
- Angular4 – No value accessor for form control
- Possible to extend types in Typescript?
- No overload matches this call. Type ‘string’ is not assignable to type ‘Signals’
- RegExp in TypeScript
- How do you format a Date/Time in TypeScript?
- npm WARN … requires a peer of … but none is installed. You must install peer dependencies yourself
- What is ViewChild in Angular2?
- How do I check whether an array contains a string in TypeScript?
- Uncaught ReferenceError: define is not defined typescript
- String.Format not work in TypeScript
- declaration or statement expected javascript/typescript
- TypeError: Class extends value undefined is not a function or null
- Can’t perform a React state update on an unmounted component
- How to connect signalR from angularJs
- Angular 4 setTimeout() with variable delay and wait
- How do I download a file with Angular2 or greater
- typescript – cloning object
- Angular 2 Sort table columns
- How to fix javascript .toFixed is not a Function error
- How do I install Angular 2 using NPM?
- How can I access and process nested objects, arrays or JSON?
- Cannot set property ‘innerHTML’ of null
- How to replace innerHTML of a div using jQuery?
- Cannot read property ‘addEventListener’ of null
- TypeError: Converting circular structure to JSON in nodejs
- Heroku: “No default language could be detected for this app” error thrown for node app
- Loop inside React JSX
- Can’t resolve module (not found) in React.js
- Is there a difference between /\s/g and /\s+/g?
- How to read an external local JSON file in JavaScript?
- Setting a backgroundImage With React Inline Styles
- Filter array to have unique values
- How do I chop/slice/trim off last character in string using Javascript?
- What is causing this error – “Fatal error: Unable to find local grunt”
- Escape quotes in JavaScript
- “Uncaught (in promise) undefined” error when using with=location in Facebook Graph API query
- Format JavaScript date as yyyy-mm-dd
- Programmatically change the src of an img tag
- Unable to preventDefault inside passive event listener
- JavaScript variable assignments from tuples
- jQuery $.cookie is not a function
- Setting background-image using jQuery CSS property
- How do I check if string contains substring?
- What does “export default” do in JSX?
- Cannot find module ‘internal/fs’ after upgrading to node 7
- How can I select an element by name with jQuery?
- How can I group data with an Angular filter?
- jQuery disable/enable submit button
- TypeError: ‘undefined’ is not an object
- Uncaught TypeError: Cannot read property ‘appendChild’ of null
- javascript setTimeout() not working
- JQuery issue “TypeError: $.getJSON is not a function”
- Retrieve the position (X,Y) of an HTML element
- command not found: jest
- How to access Session variables and set them in javascript?
- Google Maps JS API v3 – Simple Multiple Marker Example
- HTML onchange (this.value)
- socket.io – ReferenceError: io is not defined
- What is the difference D3 datum vs. data?
- How to check if a number is between two values?
- How to determine if Javascript array contains an object with an attribute that equals a given value?
- Download a file by jQuery.Ajax
- Error: Couldn’t find preset “es2015” relative to directory “/Users/username”
- Using Angular 2 without TypeScript Transpiler
- How to intercept already localized scripts
- Customizer JS API
- Loading different JS for http and https
- How to get the ToggleControl Gutenberg component working for a PHP Block
- How to append a URL parameter to all outbound URLs
- Javascript Helpers
- Rest API invalid nonce with Backbone Client
- Gutenberg block “This block appears to have been modified externally” on save
- Enqueue script if screen is greater than 500
- window.send_to_editor throws unterminated string literal error when adding image with multi line description
- Gutenberg get core data – search
- How to use Readmore.js? [closed]
- Trying to turn the Edit function into a class to be able to use React lifecycle methods
- Gutenberg richtext block vaildation failed
- Return ‘X-WP-Total’ from headers in response
- Javascript only works if browser Developer Tools panel is open
- Javascript asset not enqueuing with the rest
- WP adds long version query strings to CSS and JS files
- Load content dynamically & resize event
- ‘ajaxurl’ is undefined — wp-includes/js/wp-lists.js
- Taxonomy Template displaying different color post tag js
- Defer Parsing of “createjs.min.js” not working
- Why doesn’t my JS script load?
- Javascript Injection on my WordPress Site
- WordPress Gutenberg Ajax request
- how to refresh page after user logout with ‘wp_logout_url’?