Get first or default value from Array - Node.js Array

Node.js examples for Array:Array Value

Description

Get first or default value from Array

Demo Code

Array.prototype.firstOrDefault = function(predicateFunction) {
    if (this.length == 0) {
        return null;
    }//from  ww w .  j  av  a2 s .c o  m
    if (predicateFunction == undefined || predicateFunction == null) {
        return this[0];
    }
    this.each(function() {
        if (predicateFunction.call(this)) {
            return item;
        }
    });
    return null;
};

Related Tutorials