【问题标题】:VueJS2 sort JSON arrayVueJS2 排序 JSON 数组
【发布时间】:2017-10-28 14:18:37
【问题描述】:

我正在尝试从 JSON 文件 a.json 中对 JSON 数组进行排序。

数组是这样来的:

{"data":
    [
      {"exception":"",
      "name":"Data Server1",
      "alias":"TOR-Server",
      "delayed":"NO",
      },
      {"exception":"",
      "name":"Data Server2",
      "alias":"FRA-Server",
      "delayed":"NO",
      }
    ]

我需要按“别名”对数据进行排序,来自带有 Vue JS 的 JSON 文件。

这是我在 Vue JS 中显示的 Javascript 代码

<script type="text/javascript">

    var app = new Vue({
    el: '#app',
    data: {
    json: null
    },

     computed: {

      sorted: function() {
            setTimeout(function() {
            var app = this.app.json
            if (app.length >= 6)
            return app;
              }, 5000);
            }
    },

    methods: {
        toggleClass(){
            if (this.failed == true){
                this.failed = false;    
            }
            else
            {
                this.failed = true; 
            }
        }
    }

但是 Sorted 函数不起作用,如果我尝试在 sorted 数组中显示服务器,我会收到一个空白页。

而我在 HTML 页面中的 for 循环是:

<div class="mainbutton" v-for="(server, index) in json.data ">

希望这是有道理的,我可以让它工作。

【问题讨论】:

    标签: javascript arrays json sorting vuejs2


    【解决方案1】:

    您可以在计算后使用Array.sort 来返回排序后的数组。

    例如,要按name 属性对数组的项进行排序,可以调用:this.json.sort((t1,t2) =&gt; t1.name &lt; t2.name ? -1 : 1)。

    这是一个有效的 sn-p:

    var app = new Vue({
      el: '#app',
      data: {
        json: [
          {
            "exception": "",
            "name": "Data Server3",
            "alias": "TOR-Server",
            "delayed": "NO",
          },
          {
            "exception": "",
            "name": "Data Server1",
            "alias": "TOR-Server",
            "delayed": "NO",
          },
          {
            "exception": "",
            "name": "Data Server2",
            "alias": "FRA-Server",
            "delayed": "NO",
          }
        ]
    
      },
    
      computed: {
        sortedJson: function() {
          return this.json.sort((t1,t2) => t1.name < t2.name ? -1 : 1);
        },
        sorted: function() {
          setTimeout(function() {
            var app = this.app.json
            if (app.length >= 6)
              return app;
          }, 5000);
        }
      },
    
      methods: {
        toggleClass() {
          if (this.failed == true) {
            this.failed = false;
          } else {
            this.failed = true;
          }
        }
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.2/vue.min.js"></script>
    
    <div id="app">
      <div class="mainbutton" v-for="(server, index) in sortedJson ">
        {{server.name }}
      </div>
    </div>

    【讨论】:

    • 您好,感谢您的精彩剪辑,它有效。虽然我看到现在所有数组都已排序,但我不知道如何为 Vue JS v-for 循环设置超时,因为我们需要先等待从 JSON 文件中加载数据,然后再对数据进行排序然后获取排序后的数组并显示它?如何为 v-for 循环设置超时?
    • @Phoenix 你真的不必等待。文件是同步加载的,在这种情况下,数组仅在此之后排序。或者,如果文件是异步加载的,在这种情况下,您应该在 promise 解决时对数组进行排序。
    猜你喜欢
    • 2018-06-03
    • 2020-06-13
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多