1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- /** @format */
- interface Array<T> {
- /**
- * 增删查改
- */
- updateArrayByOperate(operateArray: any[], operate: number, key: string)
- /**
- * 根据size分割数组为size份
- * @param arr
- * @param size
- */
- splitArrBySize(size: number): any[]
- /**
- * 打乱顺序
- */
- outOrder()
- }
- Array.prototype.updateArrayByOperate = function (operateArray: any[], operate: number, key: string) {
- // 1增加 2删除 3修改 4全删
- if (operate == 1) {
- operateArray.forEach(value => {
- this.push(value)
- })
- } else if (operate == 2) {
- operateArray.forEach(value => {
- this.splice(
- this.findIndex(value1 => value1[key] == value[key]),
- 1,
- )
- })
- } else if (operate == 3) {
- operateArray.forEach(value => {
- this[this.findIndex(value1 => value1[key] == value[key])] = value
- })
- } else if (operate == 4) {
- this.splice(0, this.length)
- }
- }
- Array.prototype.splitArrBySize = function (size: number): any[] {
- let arr = []
- for (let i = 0; i < this.length; i += size) {
- arr.push(this.slice(i, i + size))
- }
- return arr
- }
- Array.prototype.outOrder = function () {
- this.sort(() => Math.randomRangeFloat(-0.5, 0.5))
- }
|