1/** 2 * Created by Liu.Jun on 2020/4/25 10:53. 3 */ 4 5// 通过 index 上移 6export function moveUpAt(target, index) { 7 if (index === 0) return false; 8 const item = target[index]; 9 const newItems = [item, target[index - 1]]; 10 return target.splice(index - 1, 2, ...newItems); 11} 12 13// 通过 index 下移动 14export function moveDownAt(target, index) { 15 if (index === target.length - 1) return false; 16 const item = target[index]; 17 const newItems = [target[index + 1], item]; 18 return target.splice(index, 2, ...newItems); 19} 20 21// 移除 22export function removeAt(target, index) { 23 // 移除数组中指定位置的元素,返回布尔表示成功与否 24 return !!target.splice(index, 1).length; 25} 26 27// 数组填充对象 28export function fillObj(target, data) { 29 // 简单复制 异常直接抛错 30 try { 31 if (typeof data === 'object') { 32 return target.fill(null).map(() => JSON.parse(JSON.stringify(data))); 33 } 34 } catch (e) { 35 // nothing ... 36 } 37 38 // 默认返回一个 undefined 39 return undefined; 40} 41 42// 切割分为多个数组 43export function cutOff(target, cutOffPointIndex) { 44 return target.reduce((preVal, curVal, curIndex) => { 45 preVal[curIndex > cutOffPointIndex ? 1 : 0].push(curVal); 46 return preVal; 47 }, [[], []]); 48} 49 50// 数组交集 51export function intersection(arr1, arr2) { 52 return arr1.filter(item => arr2.includes(item)); 53} 54