数据转换
函数名 | 描述 | 类型 | 参数 | 返回值 |
---|---|---|---|---|
arrayToMap | 数组转Map | (data, prop) => Map | (data:需转换的数组, prop: 字段) | Map |
mapToObject | Map转键值对 | (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 |
示例
函数名 | 例子 | 返回值 |
---|---|---|
arrayToMap | arrayToMap([{ a: 'aa' }], 'a') | { "Map(1)": { "aa =>": { "a": "aa" } } } |
mapToObject | mapToObject(new Map().set('a', 1).set('b', 2)) | { "a": 1, "b": 2 } |
arrayToKv | arrayToKv([ { "key": "a", "value": "a" }, { "key": "b", "value": "b" } ]) | { "a": "a", "b": "b" } |
arrayToKv | arrayToKv([ { "id": "a", "name": "a" }, { "id": "b", "name": "b" } ], "id","name") | { "a": "a", "b": "b" } |
kvToArray | kvToArray({ a: 'a', b: 'b' }) | [ { "key": "a", "value": "a" }, { "key": "b", "value": "b" } ] |
kvToArray | kvToArray({ a: 'a', b: 'b' }, 'id', 'name') | [ { "id": "a", "name": "a" }, { "id": "b", "name": "b" } ] |
dedupArray | dedupArray([1, false, 'aa', 3, 3,undefined, null, {}, []]) | [ 1, false, "aa", 3, null, null, {}, [] ] |
toArray | toArray({ a: 1, b: '2' }) | [ { "a": 1, "b": "2" } ] |
zipObject | zipObject({ a: 1, b: '2',c:null,d:undefined }) | { "a": 1, "b": "2" } |
omit | omit({ a: 'a', b: 'b' }, ['a']) | { "b": "b" } |
pick | pick({ a: 'a', b: 'b' }, ['a']) | { "a": "a" } |
trim | trim([' a ', '1 ', ' 1 ']) | [ "a", "1", "1" ] |
sum | sum([{ age: 11 }, { age: 22 }], 'age') | 33 |
avg | avg([{ age: 11 }, { age: 22 }], 'age') | 16.5 |
splitParser | splitParser('1,2,3,4') | [ "1", "2", "3", "4" ] |
splitParser | splitParser('1-2-3-4') | [ "1-2-3-4" ] |
splitParser | splitParser('1-2-3-4', '-') | [ "1", "2", "3", "4" ] |
splitStringify | splitStringify(['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>