Skip to content

数据转换

函数名描述类型参数返回值
arrayToMap数组转Map(data, prop) => Map(data:需转换的数组, prop: 字段)Map
mapToObjectMap转键值对(map:Map) => object (map: Map对象 )object
arrayToKv数组转键值对(data:array, key:string=key,value:string=value) => object(data:转换的数组, key:键,value:值)object
kvToArray键值对转数组(data:object, key:string=key,value:string=value) => array(data:转换的对象, key:键,value:值)array
dedupArray数组去重(data: T[], prop: keyof T) => array(data: 需去重的数字, prop: 去重的字段)array
toArray转换数组(val: any)=> array(val:需转换成数组的值)array
zipObject压缩对称,清除值为undefined或null的属性(obj:object)=> object(obj:需压缩的对象)object
omit对象排除属性(target:object,keys:string[])=> object( target:需要处理的对象, keys:需要排除的属性名称)object
pick对象提取属性(target:object,keys:string[])=> object( target:需要处理的对象, keys:需要挑选的属性名称)object
trim递归对象或数组清除文本类型值的两边空格(val:object| array)=> object|array(val:需要处理的数据)object|array
sum求合计(list:array,field:string)=> number(list: 需处理的对象,field: 需合并的属性)number
avg求平均值(list:array,field:string)=> number(list: 需处理的对象,field: 需合并的属性)number
splitParser字符串分隔转换成数组(val:string, flag:string)=> array(val:需处理的数据, flag:以那种方式 默认',')array
splitStringify数组转换为字符串分隔(val:array, flag:string)=> string(val:需处理的数据, flag:以那种方式 默认',')string

示例

数据转换例子
函数名例子返回值
arrayToMaparrayToMap([{ a: 'aa' }], 'a'){ "Map(1)": { "aa =>": { "a": "aa" } } }
mapToObjectmapToObject(new Map().set('a', 1).set('b', 2)){ "a": 1, "b": 2 }
arrayToKvarrayToKv([ { "key": "a", "value": "a" }, { "key": "b", "value": "b" } ]){ "a": "a", "b": "b" }
arrayToKvarrayToKv([ { "id": "a", "name": "a" }, { "id": "b", "name": "b" } ], "id","name"){ "a": "a", "b": "b" }
kvToArraykvToArray({ a: 'a', b: 'b' })[ { "key": "a", "value": "a" }, { "key": "b", "value": "b" } ]
kvToArraykvToArray({ a: 'a', b: 'b' }, 'id', 'name')[ { "id": "a", "name": "a" }, { "id": "b", "name": "b" } ]
dedupArraydedupArray([1, false, 'aa', 3, 3,undefined, null, {}, []])[ 1, false, "aa", 3, null, null, {}, [] ]
toArraytoArray({ a: 1, b: '2' })[ { "a": 1, "b": "2" } ]
zipObjectzipObject({ a: 1, b: '2',c:null,d:undefined }){ "a": 1, "b": "2" }
omitomit({ a: 'a', b: 'b' }, ['a']){ "b": "b" }
pickpick({ a: 'a', b: 'b' }, ['a']){ "a": "a" }
trimtrim([' a ', '1 ', ' 1 '])[ "a", "1", "1" ]
sumsum([{ age: 11 }, { age: 22 }], 'age')33
avgavg([{ age: 11 }, { age: 22 }], 'age')16.5
splitParsersplitParser('1,2,3,4')[ "1", "2", "3", "4" ]
splitParsersplitParser('1-2-3-4')[ "1-2-3-4" ]
splitParsersplitParser('1-2-3-4', '-')[ "1", "2", "3", "4" ]
splitStringifysplitStringify(['1', '2', '3', '4'], '-')1-2-3-4

<template>
  <div>
    <config-table title="数据转换例子" :list="list"></config-table>
  </div>
</template>

<script setup lang="ts">
  import {
    arrayToMap,
    mapToObject,
    arrayToKv,
    kvToArray,
    dedupArray,
    toArray,
    zipObject,
    omit,
    pick,
    trim,
    sum,
    avg,
    splitParser,
    splitStringify
  } from '@vtj/utils';

  import ConfigTable from '$/components/configTable.vue';

  const list = [
    {
      name: 'arrayToMap',
      example: "arrayToMap([{ a: 'aa' }], 'a')",
      return: arrayToMap([{ a: 'aa' }], 'a')
    },
    {
      name: 'mapToObject',
      example: "mapToObject(new Map().set('a', 1).set('b', 2))",
      return: mapToObject(new Map().set('a', 1).set('b', 2))
    },
    {
      name: 'arrayToKv',
      example:
        'arrayToKv([ { "key": "a", "value": "a" }, { "key": "b", "value": "b" } ])',
      return: arrayToKv([
        { key: 'a', value: 'a' },
        { key: 'b', value: 'b' }
      ])
    },
    {
      name: 'arrayToKv',
      example:
        'arrayToKv([ { "id": "a", "name": "a" }, { "id": "b", "name": "b" } ], "id","name")',
      return: arrayToKv(
        [
          { id: 'a', name: 'a' },
          { id: 'b', name: 'b' }
        ],
        'id',
        'name'
      )
    },
    {
      name: 'kvToArray',
      example: "kvToArray({ a: 'a', b: 'b' })",
      return: kvToArray({ a: 'a', b: 'b' })
    },
    {
      name: 'kvToArray',
      example: "kvToArray({ a: 'a', b: 'b' }, 'id', 'name')",
      return: kvToArray({ a: 'a', b: 'b' }, 'id', 'name')
    },
    {
      name: 'dedupArray',
      example: "dedupArray([1, false, 'aa', 3, 3,undefined, null, {}, []])",
      return: dedupArray([1, false, 'aa', 3, 3, undefined, null, {}, []])
    },
    {
      name: 'toArray',
      example: "toArray({ a: 1, b: '2' })",
      return: toArray({ a: 1, b: '2' })
    },
    {
      name: 'zipObject',
      example: "zipObject({ a: 1, b: '2',c:null,d:undefined })",
      return: zipObject({ a: 1, b: '2', c: null, d: undefined })
    },
    {
      name: 'omit',
      example: "omit({ a: 'a', b: 'b' }, ['a'])",
      return: omit({ a: 'a', b: 'b' }, ['a'])
    },
    {
      name: 'pick',
      example: "pick({ a: 'a', b: 'b' }, ['a'])",
      return: pick({ a: 'a', b: 'b' }, ['a'])
    },
    {
      name: 'trim',
      example: "trim([' a ', '1       ', ' 1 '])",
      return: trim([' a ', '1               ', ' 1 '])
    },
    {
      name: 'sum',
      example: "sum([{ age: 11 }, { age: 22 }], 'age')",
      return: sum([{ age: 11 }, { age: 22 }], 'age')
    },
    {
      name: 'avg',
      example: "avg([{ age: 11 }, { age: 22 }], 'age')",
      return: avg([{ age: 11 }, { age: 22 }], 'age')
    },
    {
      name: 'splitParser',
      example: "splitParser('1,2,3,4')",
      return: splitParser('1,2,3,4')
    },
    {
      name: 'splitParser',
      example: "splitParser('1-2-3-4')",
      return: splitParser('1-2-3-4')
    },
    {
      name: 'splitParser',
      example: "splitParser('1-2-3-4', '-')",
      return: splitParser('1-2-3-4', '-')
    },
    {
      name: 'splitStringify',
      example: "splitStringify(['1', '2', '3', '4'], '-')",
      return: splitStringify(['1', '2', '3', '4'], '-')
    }
  ];
</script>

<style scoped>
  table {
    border-collapse: collapse;
    border: 2px solid rgb(140 140 140);
    font-family: sans-serif;
    letter-spacing: 1px;
    font-size: 14px;
  }

  caption {
    caption-side: top;
    padding: 10px;
    font-weight: bold;
    font-size: 22px;
  }

  thead,
  tfoot {
    background-color: rgb(228 240 245);
  }

  th {
    border: 1px solid rgb(160 160 160);
    padding: 4px;
  }
  td {
    border: 1px solid rgb(160 160 160);
    padding: 4px;
  }

  td:last-of-type {
    text-align: center;
    font-weight: bolder;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .code {
    letter-spacing: 1px;
    font-size: 16px;
    font-style: italic;
  }
</style>

Released under the MIT License.