Be the first user to complete this post

  • 0
Add to List

Array filterUntil function implementation for javascript

This is not natively available in javascscript, so I just made a tiny utility function. It returns an array of 1 item so you can use it consistently with frameworks like Rx etc which prefer to work with Arrays. This effectively also gives you an idea of how to break from a loop, especially an alternative to using a forEach.

function filterUntil(arr, condition) {
    for (var item of arr) {
        if (condition(item)) {
            return [item];

    return [];

Why is this useful?

The advantage of using this over the primitive filter is that if you only care about the first matched value, you save computation time by not iterating through the entire array.

Example usage

filterUntil([1,2,3,4], function(item) { console.log('here'); return item === 3})

Also Read:

  1. Getting started with es6 iterators and iterables
  2. position:fixed
  3. window vs document
  4. css - vertically align text
  5. Parse html response with fetch API
  6. Getting the parameters and arguments of a javascript function
  7. Error: can not find module 'underscore'
  8. es6 iterators and iterables - creating custom iterators