function insertArray(arr, val, compare, maxLen) {
//返回位置
const index =
arr.findIndex(compare)
if (index === 0
) {
return
}
if (index > 0
) {
//删除一个
arr.splice(index, 1
)
}
//再插入(unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度)
arr.unshift(val)
if (maxLen && arr.length >
maxLen) {
//pop() 方法用于删除并返回数组的最后一个元素。
arr.pop()
}
}
删除
function deleteFromArray(arr, compare) {
const index =
arr.findIndex(compare)
if (index > -1
) {
arr.splice(index, 1
)
}
}
更多专业前端知识,请上
【猿2048】www.mk2048.com
转载请注明原文地址: https://mac.8miu.com/read-57759.html