Insert element to an array - Node.js Array

Node.js examples for Array:Insert

Description

Insert element to an array

Demo Code

insert: function(ary, from, to) {
    while(from != to) {
        if(from < to) {
            helper.swap(ary, from, from + 1);
            from += 1;//from w ww.j a v  a  2s. c  o m
        } else {
            helper.swap(ary, from, from - 1);
            from -= 1;
        }
    }
},

swap: function(ary, a, b) {
    if(a < 0 || b < 0 || ary.length <= a || ary.length <= b) {
        throw new Error('IndexError ' + a + " - " + b);
    }
    var temp = ary[a];
    ary[a] = ary[b];
    ary[b] = temp;
},

Related Tutorials