您好,欢迎访问一九零五行业门户网

sort怎么对json数据排序

这次给大家带来sort怎么对json数据排序,sort对json数据排序的注意事项有哪些,下面就是实战案例,一起来看一下。
如何对后台返回过来的json数据按照数据中的某一项进行排序呢。
首先看一下排序前的json数据:
{   result:[     {       cid:1,       name:aaa,       price:1000     },{       cid:2,       name:bbb,       price:150     },{       cid:3,       name:ccc,       price:200     },{       cid:4,       name:ddd,       price:1500     },{       cid:5,       name:eee,       price:1100     }   ],   totalcount:5 }
接下来,按照json中的price进行排序并打印到控制台:
<!doctype html> <html>   <head>     <meta charset="utf-8">     <title></title>     <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>   </head>   <body>     <script type="text/javascript">       //利用jquery中的get方法获取json数据       $.get(exp.json,,function(data){         var newdata=data.result         //根据价格(price)排序         function sortprice(a,b){           return a.price-b.price         }         //利用js中的sort方法         newdata.sort(sortprice);         //打印排序后的数据到控制台         console.log(newdata);       })     </script>   </body> </html>
这样就完成了按照price对json数据的排序,在控制台查看排序结果如下:
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
vue对eltablecolumn进行扩展
在vue中watch方法使用详解
以上就是sort怎么对json数据排序的详细内容。
其它类似信息

推荐信息