<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<template v-for="user in userSort">
<li>姓名:{{user.name}}</li>
</template>
</div>
<script>
var v = new Vue({
el: "#app",
data: {
userInfo: [
{ name: "zhangsan" },
{ name: "aisi" },
{ name: "wangwu" },
],
},
computed: {
//过滤方法
userFilter:function(){
return this.userInfo.filter(function(user){
return user.name.match(/s/);
})
},
//排序方法
userSort:function(){
//a,b为任意参数,代表遍历的当前元素或对象(此处代表的是user)
return this.userFilter.sort(function(a,b){
return a.name.length-b.name.length;
})
}
},
});
</script>
</body>
</html>