Nodejs Array Insert After insertAfter(o, toInsert)

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

Method Source Code

Array.prototype.insertAfter = function (o, toInsert) {
    var inserted = false;
    var index = this.indexOf(o);
    //from   w  ww  .j a  v  a2s  .com
    if (index == -1) {
        return false;
    }
    else {
        if (index == this.length - 1) {
            this.push(toInsert);
            return true;
        }
        else {
            return this.insertAt(toInsert, index + 1);
        }
    }
};

Related

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