赞
踩
将数组指定索引位置的元素 移动到 目标索引位置,且不改变其他元素原本的顺序,注意这个不是对调元素位置,是移动某一个元素位置不影响其他元素顺(使用场景:拖拽改变数据的顺序,点击上下左右箭头移动元素顺序)
- //将数组指定索引位置的元素 移动到 目标索引位置
- let moveArrayElement = (arr, fromIndex, toIndex) => {
- toIndex < 0 && (toIndex = 0), toIndex > arr.length - 1 && (toIndex = arr.length - 1);
- fromIndex < 0 && (fromIndex = 0), fromIndex > arr.length - 1 && (fromIndex = arr.length - 1);
- arr.splice(toIndex, 0, arr.splice(fromIndex, 1)[0]); return arr
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。