使用文档

arrToTree(arr, idField = 'id', parentField = 'parentId', childrenField = 'children')

将数组转换为树形结构。

参数

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

返回值

树形结构数组。

示例

const arr = [
   { id: 1, parentId: null, name: 'Node 1' },
   { id: 2, parentId: 1, name: 'Node 1.1' },
   { id: 3, parentId: 1, name: 'Node 1.2' },
   { id: 4, parentId: null, name: 'Node 2' },
   { id: 5, parentId: 4, name: 'Node 2.1' },
   { id: 6, parentId: 4, name: 'Node 2.2' },
];

const tree = arrToTree(arr);
console.log(tree);
// 输出:
// [
//    {
//       id: 1,
//       parentId: null,
//       name: 'Node 1',
//       children: [
//          { id: 2, parentId: 1, name: 'Node 1.1', children: [] },
//          { id: 3, parentId: 1, name: 'Node 1.2', children: [] }
//       ]
//    },
//    {
//       id: 4,
//       parentId: null,
//       name: 'Node 2',
//       children: [
//          { id: 5, parentId: 4, name: 'Node 2.1', children: [] },
//          { id: 6, parentId: 4, name: 'Node 2.2', children: [] }
//       ]
//    }
// ]

treeToArray(tree, idKey = 'id', childrenKey = 'children')

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

参数

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

返回值

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

示例

const tree = [
   {
      id: 1,
      name: 'Node 1',
      children: [
         {
            id: 2,
            name: 'Node 1.1',
            children: []
         },
         {
            id: 3,
            name: 'Node 1.2',
            children: []
         }
      ]
   },
   {
      id: 4,
      name: 'Node 2',
      children: [
         {
            id: 5,
            name: 'Node 2.1',
            children: []
         },
         {
            id: 6,
            name: 'Node 2.2',
            children: []
         }
      ]
   }
];

const arr = treeToArray(tree);
console.log(arr);
// 输出:
// [
//    { id: 1, name: 'Node 1', level: 0 },
//    { id: 2, name: 'Node 1.1', level: 1 },
//    { id: 3, name: 'Node 1.2', level: 1 },
//    { id: 4, name: 'Node 2', level: 0 },
//    { id: 5, name: 'Node 2.1', level: 1 },
//    { id: 6, name: 'Node 2.2', level: 1 }
// ]

isEmptyArray(arr)

判断数组是否为空。

参数

  • arr:要判断的数组。

返回值

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

示例

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

isNotEmptyArray(arr)

判断数组是否不为空。

参数

  • arr:要判断的数组。

返回值

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

示例

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

mergeArrays(...arrays)

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

参数

  • ...arrays:要合并的数组。

返回值

合并后的新数组。

示例

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

sumArray(arr)

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

参数

  • arr:要计算的数组。

返回值

数组中所有元素的和。

示例

console.log(sumArray([1, 2, 3, 4, 5])); // 15

flattenArray(arr)

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

参数

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

返回值

转换后的一维数组。

示例

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

shuffleArray(arr)

将数组元素随机打乱。

参数

  • arr:要打乱的数组。

返回值

打乱后的新数组。

示例

console.log(shuffleArray([1, 2, 3, 4, 5])); // [3, 1, 4, 5, 2](随机)

removeDuplicates(arr)

删除数组中的重复元素。

参数

  • arr:要处理的数组。

返回值

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

示例

console.log(removeDuplicates([1, 2, 3, 2, 1])); // [1, 2, 3]

maxArray(arr)

找出数组中的最大值。

参数

  • arr:要处理的数组。

返回值

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

示例

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

minArray(arr)

找出数组中的最小值。

参数

  • arr:要处理的数组。

返回值

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

示例

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

averageArray(arr)

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

参数

  • arr:要计算的数组。

返回值

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

示例

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

removeEmpty(arr)

去除数组中的空值。

参数

  • arr:待处理的数组。

返回值

去除空值后的新数组。

示例

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

hasIntersection(arr1, arr2)

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

参数

  • arr1:第一个数组。
  • arr2:第二个数组。

返回值

是否存在交集。

示例

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

isArray(varible)

判断变量是否为数组类型

参数

  • varible:要检查的变量

返回值

是否为数组类型

示例

console.log(isArray([1, 2, 3])); // true
console.log(isArray({})); // false
console.log(isArray('hello')); // false
数组操作工具库:JavaScript 数组处理函数集合

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

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