【问题标题】:How can I filter and order Json data [duplicate]如何过滤和订购 Json 数据 [重复]
【发布时间】:2014-08-02 04:44:49
【问题描述】:

我得到一个这样的 ajax 成功的 json 数据。

var videolist = [
  {
    "video_id": 0,
    "video_name": "Guerrero Beard",
    "timelength": 15
  },
  {
    "video_id": 1,
    "video_name": "Hallie Key",
    "timelength": 8
  },
  {
    "video_id": 2,
    "video_name": "Pitts Lloyd",
    "timelength": 27
  },
  {
    "video_id": 3,
    "video_name": "Corine Deleon",
    "timelength": 14
  }
]

我按 timelength > 10 过滤它,它可以工作。

var result = [];
$.each(videolist, function(i, o){
    if(videolist[i].timelength >10)
        result.push(videolist[i]);
});

console.log(result);

但我还需要对其进行排序。如何对这个数组进行排序?

【问题讨论】:

    标签: javascript jquery json


    【解决方案1】:
    //filter
    var result = videolist.filter(function(item) {
        return item.timelength > 10
    }); 
    
    //sort
    var result = result.sort(function(a,b) { 
       return a.timelength - b.timelength
    });
    
    console.log(result);
    

    你也可以通过 jquery 过滤

    var filterbyjquery = $.grep( videolist, function( n, i ) {
        return n.timelength > 10;
    });
    
    console.log(filterbyjquery);
    

    fiddle

    【讨论】:

    • 不幸的是,The filter() 方法在 ie9 之前的 ie 中不起作用。
    猜你喜欢
    • 2023-03-15
    • 2015-11-03
    • 1970-01-01
    • 2020-11-21
    • 2018-06-30
    • 1970-01-01
    • 2013-04-25
    • 2017-01-21
    • 2015-06-11
    相关资源
    最近更新 更多