两个数组的对象互换

两个数组的对象互换

this.list = res.list;

this.defaultList = res.list.filter(item1 => item1.isCurrent === 1) // 默认版本

this.historyList = res.list.filter(item1 => item1.isCurrent === 0) // 其他版本

点击其他版本中的某个文件,
然后选中的其他版本中的文件和默认版本中的文件互换位置

getVersionList() { // 获取历史版本列表

想请教下大家这个该怎么做?


回答:

这个问题的关键应该是获取被点击文件在数组中的索引值,实现如下:

let defaultList = [0]

let historyList = [1,2,3,4,5,6];

// index为被点击文件在historyList数组中的索引

function setList(index) {

defaultList = historyList.splice(index, 1, defaultList[0])

}

setList(3)

console.log(defaultList, historyList)

以上是 两个数组的对象互换 的全部内容, 来源链接: utcz.com/p/936943.html

回到顶部