Variable arrayUtilsConst

arrayUtils: {
    checkIndexLegal(array, index): boolean;
    distinct(array): any[];
    distinctObjList(array, primaryKey?): any[];
    mergeObjArrayByKey(source, target, key): any[];
    shuffleArray(array): any[];
} = ...

数组相关工具方法

Type declaration

  • checkIndexLegal:function
    • 检查索引是否合法

      Parameters

      • array: any[]

        数组

      • index: number

        索引

      Returns boolean

      Example

      checkIndexLegal([1, 2, 3], 1) // true
      checkIndexLegal([1, 2, 3], 4) // false
  • distinct:function
    • 基本数据类型的数组去重

      Parameters

      • array: any[]

        数组

      Returns any[]

      去重后的数组

      Example

      distinct([1, 2, 3, 1, 2, 3]) // [1, 2, 3]
      distinct([1, 2, 3, 1, 2, 3, 5]) // [1, 2, 3, 5]
      distinct(['a', 2, 'b', 1, 2, 3, 5, 5]) // ["a", 2, "b", 1, 3, 5]
  • distinctObjList:function
    • 对象数组去重, 后面的覆盖前面的,但位置顺序不变

      Parameters

      • array: any[]

        数组

      • primaryKey: string = "id"

        主键,默认为id

      Returns any[]

      去重后的数组

      Example

      distinctObjList([{ a: 1 }, { a: 2 }, { a: 1 }], 'a') // [{ a: 1 }, { a: 2 }]
      distinctObjList([{ a: 1 }, { a: 2 }, { a: 1 }], 'b') // []
      distinctObjList([{ id: 1 }, { id: 2 }, { id: 1 }]) // [{ id: 1 }, { id: 2 }]
  • mergeObjArrayByKey:function
    • 合并2个对象数组。 只对第一级进行合并,对象中的数组,后者会覆盖前者。(数组对象合并,必须指定key,因此无法做到递归合并对象数组)

      Parameters

      • source: undefined | any[]

        源数组,最终会被修改

      • target: undefined | any[]

        合并目标

      • key: string

      Returns any[]

      []

      Example

      arrayUtils.mergeObjArrayByKey([{ test: 1, test2: 2, test4: 4, dict: [1, 2, 3] }],[{ test: 1, test2: 4, test3: 3, dict: [1, 4] }],"test")
      { test: 1, test2: 4, test3: 3, test4: 4, dict: [1, 4] }
  • shuffleArray:function
    • 随机打乱数组

      Parameters

      • array: any[]

        数组

      Returns any[]

      打乱后的数组

      Example

      shuffleArray([1, 2, 3, 4, 5]) // [ 4, 1, 2, 3, 5 ] 随机打乱后的数组
      shuffleArray(['a', 2, 'b', 4, 5]) // [ 4, 'b', 2, 'a', 5 ]

Generated using TypeDoc