Javascript 按索引交换js数组中的元素

Javascript 按索引交换js数组中的元素,javascript,arrays,Javascript,Arrays,我有一个URL数组 var list = [ "https://jsonplaceholder.typicode.com/comments/1", "https://jsonplaceholder.typicode.com/comments/2", "https://jsonplaceholder.typicode.com/comments/3", "https://jsonplaceholder.typicode.com/comments/2", "https://json

我有一个URL数组

var list = [
  "https://jsonplaceholder.typicode.com/comments/1",
  "https://jsonplaceholder.typicode.com/comments/2",
  "https://jsonplaceholder.typicode.com/comments/3",
  "https://jsonplaceholder.typicode.com/comments/2",
  "https://jsonplaceholder.typicode.com/comments/1",
  "https://jsonplaceholder.typicode.com/comments/2",
  "https://jsonplaceholder.typicode.com/comments/2",
  "https://jsonplaceholder.typicode.com/comments/2",
  "https://jsonplaceholder.typicode.com/comments/2",
];
我使用一个函数:
list.splice(firstIndex,1,list.splice(secondIndex,1,list[firstIndex])[0])

按第二个索引中的元素交换第一个索引中的元素

这不起作用,因为数组有重复的元素,请建议一种只使用索引进行操作的有效方法。 假设我们将第5个元素与第4个元素交换,它将交换第0个元素并将被替换。 问题的详细解释:

考虑将阵列渲染为图像阵列:


按照我的方法,如果第5只大象被拖放到第4只鸟上,它将与第1只大象交换,因为在我的等式ie
list.splice(secondIndex,1,list[firstIndex])[0]的第二部分返回的图像URL对于第1只大象和第5只大象是相同的。有没有一种方法可以通过使用对象拼接数组来做到这一点允许通过引用更改url,因此它会更新引用同一对象的所有数组元素。将字符串映射到{url}对象,并使用映射确保所有重复字符串指向同一对象,以便它们可以一起更新。
然后,您可以交换每个列表元素中.url属性的值

var列表=[
"https://jsonplaceholder.typicode.com/comments/1",
"https://jsonplaceholder.typicode.com/comments/2",
"https://jsonplaceholder.typicode.com/comments/3",
"https://jsonplaceholder.typicode.com/comments/2",
"https://jsonplaceholder.typicode.com/comments/1",
"https://jsonplaceholder.typicode.com/comments/2",
"https://jsonplaceholder.typicode.com/comments/2",
"https://jsonplaceholder.typicode.com/comments/2",
"https://jsonplaceholder.typicode.com/comments/2",
];
var url={};//映射以跟踪重复项
list=list.map(url=>url[url]=url[url]| |{url});//将列表字符串映射到{url}对象
//swap.url值
函数交换(列表、第一索引、第二索引){
[list[secondIndex].url,list[firstIndex].url]=[list[firstIndex].url,list[secondIndex].url];
退货清单;
}
log(list.map(({url})=>url)
.map(url=>url.slice(-1)).join(',');
log(`${.repeat(4*2)}^`);
log(swap(list,5,4).map(({url})=>url)//映射回字符串列表
.map(url=>url.slice(-1)).join(',')//格式为易于阅读的数字列表
console.log(`^^`);
log(swap(list,1,2).map(({url})=>url)//映射回字符串列表

.map(url=>url.slice(-1)).join(',')//使用对象将数字列表格式化为易于阅读的格式允许通过引用更改url,因此它会更新引用同一对象的所有数组元素。将字符串映射到{url}对象,并使用映射确保所有重复字符串指向同一对象,以便它们可以一起更新。
然后,您可以交换每个列表元素中.url属性的值

var列表=[
"https://jsonplaceholder.typicode.com/comments/1",
"https://jsonplaceholder.typicode.com/comments/2",
"https://jsonplaceholder.typicode.com/comments/3",
"https://jsonplaceholder.typicode.com/comments/2",
"https://jsonplaceholder.typicode.com/comments/1",
"https://jsonplaceholder.typicode.com/comments/2",
"https://jsonplaceholder.typicode.com/comments/2",
"https://jsonplaceholder.typicode.com/comments/2",
"https://jsonplaceholder.typicode.com/comments/2",
];
var url={};//映射以跟踪重复项
list=list.map(url=>url[url]=url[url]| |{url});//将列表字符串映射到{url}对象
//swap.url值
函数交换(列表、第一索引、第二索引){
[list[secondIndex].url,list[firstIndex].url]=[list[firstIndex].url,list[secondIndex].url];
退货清单;
}
log(list.map(({url})=>url)
.map(url=>url.slice(-1)).join(',');
log(`${.repeat(4*2)}^`);
log(swap(list,5,4).map(({url})=>url)//映射回字符串列表
.map(url=>url.slice(-1)).join(',')//格式为易于阅读的数字列表
console.log(`^^`);
log(swap(list,1,2).map(({url})=>url)//映射回字符串列表


.map(url=>url.slice(-1)).join(',')//格式化为易于阅读的数字列表
如果我没有遗漏任何内容,这应该可以完成工作
list[1]=list[2]
。您的代码很混乱。您知道splice会返回删除的元素吗?对不起,我需要交换项目而不是替换,我editedx=list[1];列表[1]=列表[2];list[2]=xImagine这些被渲染成图像,我像幻灯片放映时间线一样拖放,在这种情况下,当第5个元素被拖放到第4个元素上时,它将被第1个替换(因为
list.splice(secondIndex,1,list[firstIndex])[0]
返回一个元素),如果我没有遗漏任何内容,这应该可以完成以下工作:
list[1]=list[2]
。您的代码很混乱。您知道splice会返回删除的元素吗?对不起,我需要交换项目而不是替换,我editedx=list[1];列表[1]=列表[2];list[2]=xImagine这些被渲染成图像,我像幻灯片放映时间线一样拖拽,在这种情况下,当第5个元素被拖拽到第4个元素上时,它将被第1个元素替换(因为
list.splice(secondIndex,1,list[firstIndex])[0]
返回一个元素),我只想提到我批准了它,因为这没关系,但是-1不应该出现在其他两种交换方法中。如果您只想在不向作用域添加新变量的情况下使用更短的内容,那么可以使用destructuring方法。我还添加了拼接方法。不过老实说,我并不认为使用它们有什么好处。特别是剪接版本担心延迟,我编辑了这个问题以获得更多理解,我一定会接受并投票表决答案。请毫不犹豫地进行背书:)再次感谢。请注意,您不能将拼接用于此操作,因为您正在操作对象内部的属性值,而不是对存储在arr中的对象的引用