Move an array element from one array position to another

后端 未结 30 2494
渐次进展
渐次进展 2020-11-22 08:36

I\'m having a hard time figuring out how to move an array element. For example, given the following:

var arr = [ \'a\', \'b\', \'c\', \'d\', \'e\'];
<         


        
30条回答
  •  渐次进展
    2020-11-22 09:09

    This is a really simple method using splice

    Array.prototype.moveToStart = function(index) {
        this.splice(0, 0, this.splice(index, 1)[0]);
        return this;
      };
    

提交回复
热议问题