alitajs / autils

Awesome frontend utils library
https://alitajs.github.io/autils
17 stars 1 forks source link

关于数组操作的工具类 #4

Open xiaohuoni opened 4 years ago

xiaohuoni commented 4 years ago

Background(背景)

我在开发umi-plugin-dnd 的时候,只是简单的在根组件上包裹了上下文。 后来用到实际的项目中,我觉得应该需要将组件简单的封装。 然后,组件封装完了,我发现,如果将对数据的处理,也封装成工具类。 那会让这个组件很好用。 常用的操作无非就是,添加,删除,换位,移动等操作

Proposal(建议)

比如我觉得移动操作可以是

 const reorder = (list, startIndex, endIndex) => {
        const result = Array.from(list);
        const [moved] = result.splice(startIndex, 1);
        result.splice(endIndex, 0, moved);
        return result;
};

它总是将我需要的数据移动,而让其他数据保持原来的顺序,比如我需要把第3位移到第1位,而其他保持不变时 list = reorder(list,2,0) 简单的增加,删除,确实没有必要封装,但有意思的是,如果我希望把a数组里的第7个元素,移动到b数组的第2个元素,那么此时我比较希望能够一个更完善的工具类来支撑。 如果你有实现方案,那请你直接回复我,或者提交PR。 (我现在需要完成DragAndDrop 组件编写之后,更新 umi-plugin-dnd@3 )

poyiding commented 4 years ago

移动操作有arrry-move可以满足你,但是这个库没有进行babel 转换,可能会出现编译错误,不过我已经将他收入到这里es5-imcompatible-versions,已经解决

xiaohuoni commented 4 years ago

@poyiding 其他的有没有

poyiding commented 4 years ago

关于“如果我希望把a数组里的第7个元素,移动到b数组的第2个元素”,这个简单封装一下: function generateArray(listA=[], listB=[], findIndex, insertIndex) { const getEle = listA[findIndex-1]; if(getEle) { listB.splice(insertIndex,0,getEle); } return listB; }

wangxingkang commented 4 years ago

https://github.com/pansyjs/utils/tree/master/packages/array-move

wangxingkang commented 4 years ago

如果需要 可以 把上面 那个封装到 上面的库中

wangxingkang commented 4 years ago

@xiaohuoni

xiaohuoni commented 4 years ago

需要 添加,删除,换位,移动等操作

wangxingkang commented 4 years ago

写下你希望的API我来实现吧 还有就是起个名字