mirror of https://gitee.com/cf-fz/WebCAD.git
parent
50a87fd8a8
commit
df5ce0ff62
@ -0,0 +1,106 @@
|
|||||||
|
/**
|
||||||
|
* 删除数组中指定的元素,返回数组本身
|
||||||
|
*
|
||||||
|
* @param {Array<any>} arr 需要操作的数组
|
||||||
|
* @param {*} el 需要移除的元素
|
||||||
|
*/
|
||||||
|
export function arrayRemove<T>(arr: Array<T>, el: T): Array<T>
|
||||||
|
{
|
||||||
|
let j = 0;
|
||||||
|
for (let i = 0, l = arr.length; i < l; i++)
|
||||||
|
{
|
||||||
|
if (arr[i] !== el)
|
||||||
|
{
|
||||||
|
arr[j++] = arr[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
arr.length = j;
|
||||||
|
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function arrayRemoveOnce<T>(arr: Array<T>, el: T): Array<T>
|
||||||
|
{
|
||||||
|
let j = 0;
|
||||||
|
let isErase = false;
|
||||||
|
for (let i = 0, l = arr.length; i < l; i++)
|
||||||
|
{
|
||||||
|
if (isErase || arr[i] !== el)
|
||||||
|
{
|
||||||
|
arr[j++] = arr[i];
|
||||||
|
}
|
||||||
|
else isErase = true;
|
||||||
|
}
|
||||||
|
arr.length = j;
|
||||||
|
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除通过函数校验的元素
|
||||||
|
*
|
||||||
|
* @param {(e: T) => boolean} checkFuntion 校验函数
|
||||||
|
* @memberof Array
|
||||||
|
*/
|
||||||
|
export function arrayRemoveIf<T>(arr: Array<T>, checkFuntion: (e: T) => boolean): Array<T>
|
||||||
|
{
|
||||||
|
let j = 0;
|
||||||
|
for (let i = 0, l = arr.length; i < l; i++)
|
||||||
|
{
|
||||||
|
if (!checkFuntion(arr[i]))
|
||||||
|
{
|
||||||
|
arr[j++] = arr[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
arr.length = j;
|
||||||
|
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function arrayFirst<T>(arr: Array<T>): T
|
||||||
|
{
|
||||||
|
return arr[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function arrayLast<T>(arr: Array<T>): T
|
||||||
|
{
|
||||||
|
return arr[arr.length - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function arraySortByNumber<T>(arr: Array<T>): Array<T>
|
||||||
|
{
|
||||||
|
arr.sort(sortNumberCompart);
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对排序好的数组进行去重操作
|
||||||
|
*
|
||||||
|
* @param {(e1, e2) => boolean} [checkFuction] 校验对象相等函数
|
||||||
|
*/
|
||||||
|
|
||||||
|
export function arrayRemoveDuplicateBySort<T>(arr: Array<T>, checkFuction?: (e1, e2) => boolean): Array<T>
|
||||||
|
{
|
||||||
|
checkFuction = checkFuction || checkEqual;
|
||||||
|
let j = 1;
|
||||||
|
for (let i = 1, l = arr.length; i < l; i++)
|
||||||
|
{
|
||||||
|
if (!checkFuction(arr[i], arr[j - 1]))
|
||||||
|
{
|
||||||
|
arr[j++] = arr[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
arr.length = j;
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
function sortNumberCompart(e1, e2)
|
||||||
|
{
|
||||||
|
return e1 - e2;
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkEqual(e1, e2): boolean
|
||||||
|
{
|
||||||
|
return e1 === e2;
|
||||||
|
}
|
@ -1 +0,0 @@
|
|||||||
//导入其他js模块
|
|
Loading…
Reference in new issue