• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

vue数组对象极快获取最大值和最小值linq插件各种常用好用方法,提高开发效率

武飞扬头像
请叫我欧皇i
帮助2

需求:因后端传入的数据过多,前端需要在数组中某一值的的最大值和最小值计算,平常用的最多的不就是遍历之后再比对吗,或者用sort方法等实现,同事交了我一招,一句话就可以获取到数组对象中最大值和最小值,那就是用linq插件,确实好用,用法也很简单,故而分享给大家~

1.求数组对象的某一属性的最大值和最小值

方法一,使用linq插件

1.1先下载
npm install linq -S
1.2导入,可以局部或者全局,根据自己的需求来
import Enumerable from 'linq';
1.3使用
  1.  
    const items: [
  2.  
    { id: 0, value: -5 },
  3.  
    { id: 1, value: 10 },
  4.  
    { id: 2, value: -15 },
  5.  
    { id: 3, value: 44 },
  6.  
    { id: 4, value: 44 },
  7.  
    { id: 5, value: -5 }
  8.  
    ],
  9.  
    // 筛选最大值和最小值
  10.  
    const min = Enumerable.from(this.items).min(item => item.value);
  11.  
    const max = Enumerable.from(this.items).max(item => item.value);

学新通

方法二,使用math实现,这个方法没上个好,要进行筛选

  1.  
    let arr=[2,4,56,5,7,33];
  2.  
    var obj=Math.max.apply(null,arr);
  3.  
    var min=Math.min.apply(null,arr);
  4.  
    console.log(obj,'最大值')
  5.  
    console.log(min,'最大值')

 2.根据条件筛选所需值

  1.  
    // 条件筛选出偶数,得到的值一定是满足where中条件的
  2.  
    const arr = [1, 2, 3, 4, 5];
  3.  
    this.result = Enumerable.from(arr)
  4.  
    .where(x => x % 2 === 0)
  5.  
    .toArray();
  6.  
    console.log(this.result); // [2, 4]

3.数组对象去重

  1.  
     
  2.  
    const items= [
  3.  
    { id: 0, value: -5 },
  4.  
    { id: 1, value: 10 },
  5.  
    { id: 2, value: -15 },
  6.  
    { id: 3, value: 44 },
  7.  
    { id: 4, value: 44 },
  8.  
    { id: 5, value: -5 }
  9.  
     
  10.  
    // { id: 3, value: "-220" }
  11.  
    ],
  12.  
    // linq的去重
  13.  
    this.quchong = Enumerable.from(this.items)
  14.  
    .distinct(item => item.value)
  15.  
    .toArray();
学新通

学新通

 4.筛选查询特定的值,就是filter的功能

  1.  
    // 筛选查询特定的值
  2.  
    this.select = Enumerable.from(this.items)
  3.  
    .select(item => item.value)
  4.  
    .toArray();

学新通

 5.升序降序功能

  1.  
    var myList = [
  2.  
    { Name: 'Jim', Age: 20 },
  3.  
    { Name: 'Kate', Age: 21 },
  4.  
    { Name: 'Lilei', Age: 18 },
  5.  
    { Name: 'John', Age: 14 },
  6.  
    { Name: 'LinTao', Age: 25 }
  7.  
    ];
  8.  
    this.orderByup = Enumerable.from(this.items)
  9.  
    .orderBy(x => x.value)
  10.  
    .toArray(); //升序
  11.  
    this.orderBydown = Enumerable.from(myList)
  12.  
    .orderByDescending(x => x.Age)
  13.  
    .toArray(); //降序

学新通

 6.完整例子代码

  1.  
    <template>
  2.  
    <div class="content-box">
  3.  
    <div class="container">
  4.  
    <span>原数组{{ items }}</span>
  5.  
    <br />
  6.  
    <span>最小值:{{ min }},最大值:{{ max }}</span>
  7.  
    <br />
  8.  
    <span>筛选后的值{{ result }}</span>
  9.  
    <br />
  10.  
    <span>去重后的数组{{ quchong }}</span>
  11.  
    <br />
  12.  
    <span>只要value的值{{ select }}</span>
  13.  
    <br />
  14.  
    <span>升序:{{ orderByup }}</span>
  15.  
    <br />
  16.  
    降序:{{ orderBydown }}
  17.  
    </div>
  18.  
    </div>
  19.  
    </template>
  20.  
     
  21.  
    <script>
  22.  
    import Enumerable from 'linq';
  23.  
    export default {
  24.  
    data() {
  25.  
    return {
  26.  
    items: [
  27.  
    { id: 0, value: -5 },
  28.  
    { id: 1, value: 10 },
  29.  
    { id: 2, value: -15 },
  30.  
    { id: 3, value: 44 },
  31.  
    { id: 4, value: 44 },
  32.  
    { id: 5, value: -5 }
  33.  
     
  34.  
    // { id: 3, value: "-220" }
  35.  
    ],
  36.  
    min: 0,
  37.  
    max: 0,
  38.  
    result: [],
  39.  
    quchong: [],
  40.  
    select: [],
  41.  
    groupBy: [],
  42.  
    orderByup: [],
  43.  
    orderBydown: []
  44.  
    };
  45.  
    },
  46.  
    mounted() {
  47.  
    this.query();
  48.  
    },
  49.  
    methods: {
  50.  
    query() {
  51.  
    // 筛选最大值和最小值
  52.  
    this.min = Enumerable.from(this.items).min(item => item.value);
  53.  
    this.max = Enumerable.from(this.items).max(item => item.value);
  54.  
    // 条件筛选出偶数,得到的值一定是满足where中条件的
  55.  
    const arr = [1, 2, 3, 4, 5];
  56.  
    this.result = Enumerable.from(arr)
  57.  
    .where(x => x % 2 === 0)
  58.  
    .toArray();
  59.  
    console.log(this.result); // [2, 4]
  60.  
     
  61.  
    // linq的去重
  62.  
    this.quchong = Enumerable.from(this.items)
  63.  
    .distinct(item => item.value)
  64.  
    .toArray();
  65.  
     
  66.  
    // 筛选查询特定的值
  67.  
    this.select = Enumerable.from(this.items)
  68.  
    .select(item => item.value)
  69.  
    .toArray();
  70.  
    // 分组
  71.  
    var myList = [
  72.  
    { Name: 'Jim', Age: 20 },
  73.  
    { Name: 'Kate', Age: 21 },
  74.  
    { Name: 'Lilei', Age: 18 },
  75.  
    { Name: 'John', Age: 14 },
  76.  
    { Name: 'LinTao', Age: 25 }
  77.  
    ];
  78.  
    this.orderByup = Enumerable.from(this.items)
  79.  
    .orderBy(x => x.value)
  80.  
    .toArray(); //升序
  81.  
    this.orderBydown = Enumerable.from(myList)
  82.  
    .orderByDescending(x => x.Age)
  83.  
    .toArray(); //降序
  84.  
     
  85.  
    var array1 = [1, 412, 5, 3, 5, 412, 7];
  86.  
    var array2 = [20, 12, 5, 5, 7, 310];
  87.  
    const except = Enumerable.from(array1).except(array2).toArray(); //结果3,412,1
  88.  
    console.log(except, '取差集,差集就是俩个数组中不相同的值');
  89.  
     
  90.  
    var array1 = [1, 412, 5, 3, 5, 412, 7];
  91.  
    var array2 = [20, 12, 5, 5, 7, 310];
  92.  
    const intersect = Enumerable.from(array1).intersect(array2).toArray();
  93.  
    //结果5,7
  94.  
    console.log(intersect, '取交集,交集就是俩个数组相同的值');
  95.  
    }
  96.  
    }
  97.  
    };
  98.  
    </script>
  99.  
     
  100.  
    <style lang="scss" scoped></style>
学新通

学新通

常用的差不多就这些了,还有一些其他方法可以自行探索~文章到此结束,希望对你有所帮助~

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhhkbeaj
系列文章
更多 icon
同类精品
更多 icon
继续加载