Move an array element from one array position to another

后端 未结 30 2492
渐次进展
渐次进展 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:04

        let oldi, newi, arr;
        
        if(newi !== oldi) {
          let el = this.arr.splice(oldi, 1);
          if(newi > oldi && newi === (this.arr.length + 2)) {
            this.arr.push("");
          }
          this.arr.splice(newi, 0, el);
          if(newi > oldi && newi === (this.arr.length + 2)) {
            this.arr.pop();
          }
        }

提交回复
热议问题