1、普通数组
方法1:
let arr = [1, 2, 3, 4]
let key = 3
data.map((item,index) => {
if(index == key){
data.unshift(data.splice(index , 1)[0]);
}
})
方法2:
let arr = [1, 2, 3, 4]
let key = 3
for (let i = 0; i < arr.length; i++) {
if (arr[i] === key) {
arr.splice(i, 1);
break;
}
}
arr.unshift(key);
2、数组对象
let data = [{
nickName: "9",
sex: "男",
userId: 9
}, {
nickName: "8",
sex: "男",
userId: 8
}, {
nickName: "7",
sex: "男",
userId: 7
}]
let userId = 7
let obj = {}
data.length && data.forEach((item, index) => {
if (item.userId == userId) {
obj = item
data.splice(index, 1)
return
}
})
data.unshift(obj)