vue数组排序


在我们工作中经常会遇到数组排序这样的东西,遇到了顺便就记录下来

 <div v-for="student,index in sortStudent" :key="index">
      {{index+1}} {{ student.name }}--{{student.age}}
 </div>

 vue代码

 student: [{
                            name: 'Sugar',
                            age: '23'
                        },
                        {
                            name: 'Yang',
                            age: '20'
                        },
                        {
                            name: 'xiucheng',
                            age: '18'
                        }
                    ]
                }

 js

computed:{
     sortStudent:function(){
            return sortByKey(this.student,'age')
          }
    }

  

//数组对象方法排序
        function sortByKey(array,key){
            return array.sort(function(a,b){
                var x=a[key];
                var y=b[key];

                return ((x<y)?-1:(x>y)?1:0)
            })
        }

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM