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