JavaScript 数组操作函数库:实用工具和示例

这是一个 JavaScript 数组操作函数库,提供各种实用工具函数,包括数组转换、判断、合并、计算、去重、排序、查找等。包含详细函数说明、参数解释和示例代码。

函数列表

| 函数名称 | 参数 | 描述 | 示例 | |---|---|---|---| | arrToTree | arr: Array, idField: String = 'id', parentField: String = 'parentId', childrenField: String = 'children' | 将数组转换为树形结构数组 | ```javascript const data = [ { id: 1, parentId: null, name: 'A' }, { id: 2, parentId: 1, name: 'B' }, { id: 3, parentId: 1, name: 'C' }, { id: 4, parentId: 2, name: 'D' }, { id: 5, parentId: 2, name: 'E' } ];

const tree = arrToTree(data); console.log(tree); // 输出: // [ // { id: 1, parentId: null, name: 'A', children: [ // { id: 2, parentId: 1, name: 'B', children: [ // { id: 4, parentId: 2, name: 'D', children: [] }, // { id: 5, parentId: 2, name: 'E', children: [] } // ] }, // { id: 3, parentId: 1, name: 'C', children: [] } // ] } // ]

| `treeToArray` | `tree: Object[], idKey: string = 'id', childrenKey: string = 'children'` | 将树形结构数组转换为一维数组 | ```javascript
const tree = [
  { id: 1, name: 'A', children: [
    { id: 2, name: 'B', children: [
      { id: 4, name: 'D', children: [] },
      { id: 5, name: 'E', children: [] }
    ] },
    { id: 3, name: 'C', children: [] }
  ] }
];

const arr = treeToArray(tree);
console.log(arr);
// 输出:
// [
//   { name: 'A', level: 0 },
//   { name: 'B', level: 1 },
//   { name: 'D', level: 2 },
//   { name: 'E', level: 2 },
//   { name: 'C', level: 1 }
// ]
``` | 
| `isEmptyArray` | `arr: Array` | 判断数组是否为空 | ```javascript
console.log(isEmptyArray([])); // true
console.log(isEmptyArray([1, 2, 3])); // false
``` | 
| `isNotEmptyArray` | `arr: Array` | 判断数组是否不为空 | ```javascript
console.log(isNotEmptyArray([])); // false
console.log(isNotEmptyArray([1, 2, 3])); // true
``` | 
| `mergeArrays` | `...arrays: Array` | 将多个数组合并成一个新数组 | ```javascript
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const arr3 = [7, 8, 9];

const mergedArr = mergeArrays(arr1, arr2, arr3);
console.log(mergedArr); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
``` | 
| `sumArray` | `arr: Array` | 计算数组中所有元素的和 | ```javascript
const arr = [1, 2, 3, 4, 5];

const sum = sumArray(arr);
console.log(sum); // 15
``` | 
| `flattenArray` | `arr: Array` | 将二维数组转换为一维数组 | ```javascript
const arr = [[1, 2], [3, 4], [5, 6]];

const flattenedArr = flattenArray(arr);
console.log(flattenedArr); // [1, 2, 3, 4, 5, 6]
``` | 
| `shuffleArray` | `arr: Array` | 将数组元素随机打乱 | ```javascript
const arr = [1, 2, 3, 4, 5];

const shuffledArr = shuffleArray(arr);
console.log(shuffledArr); // 随机打乱后的数组
``` | 
| `removeDuplicates` | `arr: Array` | 删除数组中的重复元素 | ```javascript
const arr = [1, 2, 2, 3, 4, 4, 5];

const uniqueArr = removeDuplicates(arr);
console.log(uniqueArr); // [1, 2, 3, 4, 5]
``` | 
| `maxArray` | `arr: Array` | 找出数组中的最大值 | ```javascript
const arr = [1, 2, 3, 4, 5];

const max = maxArray(arr);
console.log(max); // 5
``` | 
| `minArray` | `arr: Array` | 找出数组中的最小值 | ```javascript
const arr = [1, 2, 3, 4, 5];

const min = minArray(arr);
console.log(min); // 1
``` | 
| `averageArray` | `arr: Array` | 计算数组中所有元素的平均值 | ```javascript
const arr = [1, 2, 3, 4, 5];

const average = averageArray(arr);
console.log(average); // 3
``` | 
| `removeEmpty` | `arr: Array` | 去除数组中的空值 | ```javascript
const arr = [1, '', 2, null, 3, undefined];

const newArr = removeEmpty(arr);
console.log(newArr); // [1, 2, 3]
``` | 
| `hasIntersection` | `arr1: any[], arr2: any[]` | 判断两个数组是否有交集 | ```javascript
const arr1 = [1, 2, 3];
const arr2 = [3, 4, 5];

console.log(hasIntersection(arr1, arr2)); // true

const arr3 = [6, 7, 8];
console.log(hasIntersection(arr1, arr3)); // false
``` | 
| `isArray` | `variable: any` | 判断变量是否为数组类型 | ```javascript
console.log(isArray([])); // true
console.log(isArray(1)); // false
console.log(isArray('hello')); // false
``` | 

### 使用方法

```javascript
import { arrToTree, treeToArray, isEmptyArray, ... } from './array-utils';

贡献

欢迎您为这个函数库贡献代码!您可以通过以下方式贡献:

  • 提交问题
  • 提交代码
  • 提出建议

许可证

本函数库使用 MIT 许可证。

JavaScript 数组操作函数库:实用工具和示例

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

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