Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Array filterUntil function implementation for javascript

This is not natively avail­able in javasc­script, so I just made a tiny util­ity func­tion. It returns an array of 1 item so you can use it con­sis­tently with frame­works like Rx etc which pre­fer to work with Arrays.

This effec­tively also gives you an idea of how to break from a loop, espe­cially an alter­na­tive to using a forEach.

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

    return [];

Why is this useful?

The advan­tage of using this over the prim­i­tive fil­ter is that if you only care about the first matched value, you save com­pu­ta­tion time by not iter­at­ing through the entire array.

Exam­ple usage

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

Ryan Sukale

Ryan is just a regular guy next door trying to manage his life and finances.