1 Star 0 Fork 0

luyuan/MaxKB

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
array.ts 2.08 KB
一键复制 编辑 原始数据 按行查看 历史
wangdan-fit2cloud 提交于 2025-07-02 22:41 +08:00 . fix: bugs
/**
* 拆分数组 每n个拆分为一个数组
* @param sourceDataList 资源数据
* @param splitNum 每多少个拆分为一个数组
* @returns 拆分后数组
*/
export function splitArray<T>(sourceDataList: Array<T>, splitNum: number) {
const count =
sourceDataList.length % splitNum == 0
? sourceDataList.length / splitNum
: sourceDataList.length / splitNum + 1
const arrayList: Array<Array<T>> = []
for (let i = 0; i < count; i++) {
let index = i * splitNum
const list: Array<T> = []
let j = 0
while (j < splitNum && index < sourceDataList.length) {
list.push(sourceDataList[index++])
j++
}
arrayList.push(list)
}
return arrayList
}
/*
树形结构转平
*/
export function TreeToFlatten(treeData: any[]) {
return treeData.reduce((acc, node) => {
const { children, ...rest } = node
return [...acc, rest, ...(children ? TreeToFlatten(children) : [])]
}, [])
}
/*
从指定数组中过滤出对应的对象
*/
export function relatedObject(list: any, val: any, attr: string) {
const filterData: any = list.find((item: any) => item[attr] === val)
return filterData || null
}
// 排序
export function arraySort(list: Array<any>, property: any, desc?: boolean) {
return list.sort((a: any, b: any) => {
return desc ? b[property] - a[property] : a[property] - b[property]
})
}
// 判断对象里所有属性全部为空
export function isAllPropertiesEmpty(obj: object) {
return Object.values(obj).every(
(value) =>
value === null || typeof value === 'undefined' || (typeof value === 'string' && !value),
)
}
// 数组对象中某一属性值的集合
export function getAttrsArray(array: Array<any>, attr: string) {
return array.map((item) => {
return item[attr]
})
}
// 求和
export function getSum(array: Array<any>) {
return array.reduce((total, item) => total + item, 0)
}
// 对象数组去重
export function uniqueArray(array: Array<any>, key: string) {
const map = new Map()
return array.filter((item) => {
return !map.has(item[key]) && map.set(item[key], 1)
})
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/java_doc/MaxKB.git
git@gitee.com:java_doc/MaxKB.git
java_doc
MaxKB
MaxKB
v2

搜索帮助