Nodejs Array Insert Before insertBefore(o, toInsert)

Here you can find the source of insertBefore(o, toInsert)

Method Source Code

Array.prototype.insertBefore = function (o, toInsert) {
    var inserted = false;
    var index = this.indexOf(o);
    /*from  w w w  .  ja  v  a  2 s. c  o m*/
    if (index == -1) {
        return false;
    }
    else {
        if (index === 0) {
            this.unshift(toInsert);
            return true;
        }
        else {
            return this.insertAt(toInsert, index - 1);
        }
    }
};

Related

  1. insertBefore(index, item)
    Array.prototype.insertBefore = function(index, item) {
        if (index > this.length - 1 || index < 0) {
            throw new Error("Index out of range");
        this.splice(index, 0, item);
    };