# arrTool 数组工具

# ascOrder(arr, key):数组升序排列

import {arrTool} from '@/utils/index'

const arr = [ 1 , 8 , 9];
arrTool.ascOrder(arr);

const arr2 = [
    {"姓名":"张三",'年龄':18},
    {"姓名":"李四",'年龄':20},
    {"姓名":"王五",'年龄':35},
];
arrTool.ascOrder(arr2,'年龄');

# descOrder(arr, key):数组降序排列

用法同上

# selectProp(arr, key, unique)

将对象数组中的某个属性重组为一个新的数组
unique:boolean - 是否去除重复项

import {arrTool} from '@/utils/index'
const arr = [
    {name:'洗衣机',price:789},
    {name:'电饭煲',price:456},
    {name:'电磁炉',price:123},
    {name:'电磁炉',price:231}
];
const names = arrTool.selectProp(arr,'name');
//names:['洗衣机','电饭煲','电磁炉','电磁炉']

# selectPropUnique(arr, key)

将对象数组中的某个属性重组为一个新的数组,并去除重复项

import {arrTool} from '@/utils/index'
const arr = [
    {name:'洗衣机',price:789},
    {name:'电饭煲',price:456},
    {name:'电磁炉',price:123},
    {name:'电磁炉',price:231}
];
const names = arrTool.selectPropUnique(arr,'name');
//names:['洗衣机','电饭煲','电磁炉']

# selectSum(arr, key, bei):数值累加

bei:number - 精度控制,默认为1。如果数组中有小数,可以传入100或者10000,累加的时候会乘以这个系数,结果会除这个系数。但如果系数太大,累加的数值也比较大的话,也会有最大值溢出的问题。

import {arrTool} from '@/utils/index'
const arr1 = [
    {name:'洗衣机',price:789},
    {name:'电饭煲',price:456},
    {name:'电磁炉',price:123},
    {name:'电磁炉',price:231}
];
const arr2 = [789,456,123,231];

const total1 = arrTool.selectSum(arr1,'price');
const total2 = arrTool.selectSum(arr2);
//total1 = total2 = 1599:

# selectMin(arr)

获取数组中最小的数数值。如果数组为空则返回undefined

import {arrTool} from '@/utils/index'
const arr1 = [789,456,123,231];

const result = arrTool.selectMin(arr1);
//result = 123

# selectMax(arr)

获取数组中最大的数字。如果数组为空则返回undefined

import {arrTool} from '@/utils/index'
const arr1 = [789,456,123,231];

const result = arrTool.selectMax(arr1);
//result = 789
Last Updated: 1/9/2020, 11:04:22 AM