zoukankan      html  css  js  c++  java
  • vue里数组排序示例

    vue的默认排序方式
    <!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 userInfo">
            <li>姓名:{{user}}</li>
          </template>
        </div>
        <script>
            var v=new Vue({
                el:"#app",
                data:{
                    userInfo:['zhangsan','aisi','wangwu']
                }
            });
           v.userInfo.sort();
        </script>
      </body>
    </html>
     
    改写排序方式的示例
    <!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 userInfoSorted">
            <li>姓名:{{user.name}}</li>
          </template>
        </div>
        <script>
          var v = new Vue({
            el: "#app",
            data: {
              userInfo: [
                { name: "zhangsan" },
                { name: "aisi" },
                { name: "wangwu" },
              ],
            },
            computed: {
              userInfoSortedfunction () {
    // a,b参数代表的是user 按照 长度降序排序
                return this.userInfo.sort(function (a, b) {
                  return -(a.name.length - b.name.length);
                });
              },
            },
          });
        </script>
      </body>
    </html>
  • 相关阅读:
    【LeetCode每日一题】2020.6.28 209. 长度最小的子数组
    【《你不知道的JS(中卷①)》】三、原生函数
    【《你不知道的JS(中卷①)》】二、值
    【《你不知道的JS(中卷①)》】一、类型
    【LeetCode每日一题】2020.6.26 面试题 02.01. 移除重复节点
    【LeetCode周赛】第194场周赛
    【LeetCode每日一题】2020.6.25 139. 单词拆分
    ios网络编程(HTTP socket)
    objective c 代码块blocks完整总结二
    objective c 代码块blocks完整总结一
  • 原文地址:https://www.cnblogs.com/kukai/p/12857743.html
Copyright © 2011-2022 走看看