有没有一种更简单的方法来交换数组中的两个元素?
var a = list[x], b = list[y];
list[y] = a;
list[x] = b;
var a = list[x], b = list[y];
list[y] = a;
list[x] = b;
var swapArrayElements = function (a, x, y) {
if (a.length === 1)
return a;
a.splice(y, 1, a.splice(x, 1, a[y])[0]);
return a;
};
swapArrayElements([1, 2, 3, 4, 5], 1, 3); //=> [ 1, 4, 3, 2, 5 ]
a.splice
返回一个包含已删除元素的数组。https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/splice - XCSArray.prototype.swap = function(a, b) {
var temp = this[a];
this[a] = this[b];
this[b] = temp;
};
var myArray = [0,1,2,3,4...];
myArray.swap(4,1);
Array.prototype.swapItems = function(a, b){
if( !(a in this) || !(b in this) )
return this;
this[a] = this.splice(b, 1, this[a])[0];
return this;
}
this
,但您可以轻松修改失败的行为。export function swapItemsInArray<T>(items: T[], indexA: number, indexB: number): T[] {
const itemA = items[indexA];
const clone = [...items];
clone[indexA] = clone[indexB];
clone[indexB] = itemA;
return clone;
}
function swapElements(array, source, dest) {
return source === dest
? array : array.map((item, index) => index === source
? array[dest] : index === dest
? array[source] : item);
}
const arr = ['a', 'b', 'c'];
const s1 = swapElements(arr, 0, 1);
console.log(s1[0] === 'b');
console.log(s1[1] === 'a');
const s2 = swapElements(arr, 2, 0);
console.log(s2[0] === 'c');
console.log(s2[2] === 'a');
以下是TypeScript代码,可供快速复制粘贴:
function swapElements(array: Array<any>, source: number, dest: number) {
return source === dest
? array : array.map((item, index) => index === source
? array[dest] : index === dest
? array[source] : item);
}
var foo = [ 0, 1, 2, 3, 4, 5, 6 ];
foo.splice( a, 1, foo.splice(b,1,foo[a])[0] );
foo.splice( 3, 1, foo.splice(5,1,foo[3])[0] );
或者
foo.splice( 5, 1, foo.splice(3,1,foo[5])[0] );
两者产生相同的结果:
console.log( foo );
// => [ 0, 1, 2, 5, 4, 3, 6 ]
#splicehatersarepunks:)
// Array methods
function swapInArray(arr, i1, i2){
let t = arr[i1];
arr[i1] = arr[i2];
arr[i2] = t;
}
function moveBefore(arr, el){
let ind = arr.indexOf(el);
if(ind !== -1 && ind !== 0){
swapInArray(arr, ind, ind - 1);
}
}
function moveAfter(arr, el){
let ind = arr.indexOf(el);
if(ind !== -1 && ind !== arr.length - 1){
swapInArray(arr, ind + 1, ind);
}
}
// DOM methods
function swapInDom(parentNode, i1, i2){
parentNode.insertBefore(parentNode.children[i1], parentNode.children[i2]);
}
function getDomIndex(el){
for (let ii = 0; ii < el.parentNode.children.length; ii++){
if(el.parentNode.children[ii] === el){
return ii;
}
}
}
function moveForward(el){
let ind = getDomIndex(el);
if(ind !== -1 && ind !== 0){
swapInDom(el.parentNode, ind, ind - 1);
}
}
function moveBackward(el){
let ind = getDomIndex(el);
if(ind !== -1 && ind !== el.parentNode.children.length - 1){
swapInDom(el.parentNode, ind + 1, ind);
}
}
// Example 1: swapping array index
let arr = ["one", "two"];
[arr[0], arr[1]] = [arr[1], arr[0]];
// Output: arr = ["two", "one"]
console.log(arr);
// Example 2: swapping two variables value
let a = 10;
let b = 20;
[a, b] = [b, a];
// Output: a = 20, b = 10;
console.log(a, b);
with()
可以在一行中完成:list.with(x,list[y]).with(y,list[x])
看看我的答案和示例! - XMehdi01with()
可以在一行中完成:list.with(x,list[y]).with(y,list[x])
看看我的回答,附带一个示例! - undefined