Here you can find the source of move(from, to)
/* /* w w w . j a v a2 s . co m*/ Problem #2: Let\'s make a prototype function which can \'move\' the element of an array, and then return the result as a new one. The \'move\' function accepts two parameters. from and to, the former is the element you want to reordering, the latter is the destination index of your target element. There are two condition you should aware: If the target you assgin doesn\'t exist, return \'no target\'. If the detination index is over the length of array, it should insert undefined to the blank until it has enough length to do the movement. var arr = ["a", "b", "c"]; arr.move(0,2); // -> ["b", "c", "a"] arr.move(2,0); // -> ["c", "a", "b"] arr.move(1,5); // -> ["a", "c", undefined, undefined, undefined, "b"] arr.move(5,1); // -> \'no target\' arr // -> still ["a", "b", "c"] */ // SOLUTION TO PROBLEM #2: Array.prototype.move = function(from, to) { if (from >= this.length) return 'no target' var a = this.concat() var e = a.splice(from, 1)[0] if (to > a.length) { a[to] = e } else { a.splice(to, 0, e) } return a } // SOME EXTRA TESTING var ArrayTest = ['a', 'b', 'e'] console.log("the origin array below") console.log(ArrayTest.toString()) var newArr = ArrayTest.move(0, 2) console.log((newArr).toString() + "\nWE DID ITTT :D")
Array.prototype.move = function (from, to) {
this.splice(to, 0, this.splice(from, 1)[0]);
};
Array.prototype.move = function(from, to) { if (Object.prototype.toString.call(this) !== '[object Array]') { throw new TypeError("`this` must be Array, not " + typeof this); if (typeof from !== 'number') { throw new TypeError("argument[0] must be number, not " + typeof from); if (typeof to !== 'number') { throw new TypeError("argument[1] must be number, not " + typeof to); ...
Array.prototype.move = function (from, to) {
this.splice(to, 0, this.splice(from, 1)[0]);
};
Array.prototype.move = function(oldIndex, newIndex) { if (isNaN(newIndex) || isNaN(oldIndex) || oldIndex < 0 || oldIndex >= this.length) { return; if (newIndex < 0) { newIndex = this.length - 1; } else if (newIndex >= this.length) { newIndex = 0; this.splice(newIndex, 0, this.splice(oldIndex, 1)[0]); return newIndex; };
Array.prototype.move = function(oldIndex,newIndex){ return this.splice(newIndex, 0, this.splice(oldIndex, 1)[0]);
Array.prototype.move = function(old_index, new_index) { if (new_index >= 0 && new_index < this.length) this.splice(new_index, 0, this.splice(old_index, 1)[0]); return this; };
Array.prototype.move = function (old_index, new_index) { if (new_index >= this.length) { let k = new_index - this.length; while ((k--) + 1) { this.push(undefined); this.splice(new_index, 0, this.splice(old_index, 1)[0]); return this; ...