使用说明文档

arrToTree

将数组转换为树形结构。

参数

  • arr (Array):要转换的数组。
  • idField (String):节点 ID 字段名(默认为 'id')。
  • parentField (String):父节点 ID 字段名(默认为 'parentId')。
  • childrenField (String):子节点数组字段名(默认为 'children')。

返回值

  • Array:树形结构数组。

示例

const arr = [
   { id: 1, parentId: null },
   { id: 2, parentId: 1 },
   { id: 3, parentId: 1 },
   { id: 4, parentId: 2 },
   { id: 5, parentId: 2 },
   { id: 6, parentId: 3 },
];

const tree = arrToTree(arr);

console.log(tree);
// Output: [
//    { id: 1, parentId: null, children: [
//       { id: 2, parentId: 1, children: [
//          { id: 4, parentId: 2, children: [] },
//          { id: 5, parentId: 2, children: [] }
//       ] },
//       { id: 3, parentId: 1, children: [
//          { id: 6, parentId: 3, children: [] }
//       ] }
//    ] }
// ]

treeToArray

将树形结构转换为数组结构。

参数

  • tree (Object[]):树形结构数据,每个对象包含 idchildren 属性,children 属性为子节点数组。
  • idKey (string):节点标识属性的键名,默认为 'id'
  • childrenKey (string):子节点数组属性的键名,默认为 'children'

返回值

  • Object[]:数组结构数据,每个对象包含所有原始节点属性以及 level 属性表示节点所在层级。

示例

const tree = [
   {
      id: 1,
      children: [
         {
            id: 2,
            children: [
               { id: 4 },
               { id: 5 }
            ]
         },
         {
            id: 3,
            children: [
               { id: 6 }
            ]
         }
      ]
   }
];

const arr = treeToArray(tree);

console.log(arr);
// Output: [
//    { id: 1, level: 0 },
//    { id: 2, level: 1 },
//    { id: 4, level: 2 },
//    { id: 5, level: 2 },
//    { id: 3, level: 1 },
//    { id: 6, level: 2 }
// ]

isEmptyArray

判断数组是否为空。

参数

  • arr (Array):要判断的数组。

返回值

  • boolean:如果数组为空则返回 true,否则返回 false

示例

console.log(isEmptyArray([])); // Output: true
console.log(isEmptyArray([1, 2, 3])); // Output: false

isNotEmptyArray

判断数组是否不为空。

参数

  • arr (Array):要判断的数组。

返回值

  • boolean:如果数组不为空则返回 true,否则返回 false

示例

console.log(isNotEmptyArray([])); // Output: false
console.log(isNotEmptyArray([1, 2, 3])); // Output: true

mergeArrays

将多个数组合并成一个新数组。

参数

  • ...arrays (Array):要合并的数组。

返回值

  • Array:合并后的新数组。

示例

console.log(mergeArrays([1, 2], [3, 4], [5, 6])); // Output: [1, 2, 3, 4, 5, 6]

sumArray

计算数组中所有元素的和。

参数

  • arr (Array):要计算的数组。

返回值

  • number:数组中所有元素的和。

示例

console.log(sumArray([1, 2, 3])); // Output: 6

flattenArray

将二维数组转换为一维数组。

参数

  • arr (Array):要转换的二维数组。

返回值

  • Array:转换后的一维数组。

示例

console.log(flattenArray([[1, 2], [3, 4], [5, 6]])); // Output: [1, 2, 3, 4, 5, 6]

shuffleArray

将数组元素随机打乱。

参数

  • arr (Array):要打乱的数组。

返回值

  • Array:打乱后的新数组。

示例

console.log(shuffleArray([1, 2, 3, 4, 5, 6])); // Output: [4, 1, 6, 2, 5, 3]

removeDuplicates

删除数组中的重复元素。

参数

  • arr (Array):要处理的数组。

返回值

  • Array:删除重复元素后的新数组。

示例

console.log(removeDuplicates([1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5])); // Output: [1, 2, 3, 4, 5]

maxArray

找出数组中的最大值。

参数

  • arr (Array):要处理的数组。

返回值

  • number|null:数组中的最大值,如果数组为空则返回 null

示例

console.log(maxArray([1, 2, 3, 4, 5])); // Output: 5
console.log(maxArray([])); // Output: null

minArray

找出数组中的最小值。

参数

  • arr (Array):要处理的数组。

返回值

  • number|null:数组中的最小值,如果数组为空则返回 null

示例

console.log(minArray([1, 2, 3, 4, 5])); // Output: 1
console.log(minArray([])); // Output: null

averageArray

计算数组中所有元素的平均值。

参数

  • arr (Array):要计算的数组。

返回值

  • number|null:数组中所有元素的平均值,如果数组为空则返回 null

示例

console.log(averageArray([1, 2, 3, 4, 5])); // Output: 3
console.log(averageArray([])); // Output: null

removeEmpty

去除数组中的空值。

参数

  • arr (Array):要处理的数组。

返回值

  • Array:去除空值后的新数组。

示例

console.log(removeEmpty([1, null, undefined, '', 2, 3, false])); // Output: [1, 2, 3, false]

hasIntersection

判断两个数组是否有交集。

参数

  • arr1 (any[]):第一个数组。
  • arr2 (any[]):第二个数组。

返回值

  • boolean:是否存在交集。

示例

console.log(hasIntersection([1, 2, 3], [2, 4, 6])); // Output: true
console.log(hasIntersection([1, 2, 3], [4, 5, 6])); // Output: false

isArray

判断变量是否为数组类型。

参数

  • variable (any):要检查的变量。

返回值

  • boolean:是否为数组类型
JavaScript 数组操作函数库:包含树形结构转换、数组去重、排序等功能

原文地址: https://www.cveoy.top/t/topic/lKMi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录