您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
今天小编给大家分享一下js中怎么对json数组进行排序的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
废话不多说,直接上代码
// 升序排列 function up(a, b) { return a.val-b.val }, // 降序排列 function down(a, b) { return b.val-a.val }, // sort 会直接对原数据排序 testJson.sort(up)
例如:
let test=[{"val":100},{"val":70},{"val":50},{"val":20},{"val":10}] test=test.sort(up)
let newArray = [ {product: "1", matno: "420200011480006",salebillno:"xsdd2733-8", sales: "800"}, {product: "2", matno: "420200011480007",salebillno:"xsdd2733-2", sales: "300"}, {product: "3", matno: "420200011480006",salebillno:"xsdd2733-5", sales: "100"}, {product: "4", matno: "420200011480006",salebillno:"xsdd2733-3", sales: "200"}, {product: "5", matno: "420200011480008",salebillno:"xsdd2738", sales: "600"}, {product: "6", matno: "420200011480010",salebillno:"xsdd2734", sales: "400"}, {product: "7", matno: "420200011480009",salebillno:"xsdd2735", sales: "700"}, {product: "8", matno: "420200011480001",salebillno:"xsdd2737", sales: "500"} ] /**数组根据数组对象中的某个属性值进行排序的方法 * 使用例子:newArray.sort(sortByArr(['matno'],true)) //表示根据matno属性降序排列;若第二个参数不传递,默认表示升序排序 * @param attr 排序的属性 ['matno','salebillno'...],根据一个字段或者多个字段排序 * @param rev true表示升序排列,false降序排序 * */ function sortByArr(arr, rev) { if (rev == undefined) { rev = 1; } else { rev = (rev) ? 1 : -1; } return function(a, b) { for (var i = 0; i < arr.length; i++) { let attr = arr[i] if (a[attr] != b[attr]) { if (a[attr] > b[attr]) { return rev * 1; } else { return rev * -1; } } } } } /**升序**/ newArray.sort(sortByArr(['matno','salebillno'],true)) [{product: '8', matno: '420200011480001', salebillno: 'xsdd2737', sales: '500'} {product: '4', matno: '420200011480006', salebillno: 'xsdd2733-3', sales: '200'} {product: '3', matno: '420200011480006', salebillno: 'xsdd2733-5', sales: '100'} {product: '1', matno: '420200011480006', salebillno: 'xsdd2733-8', sales: '800'} {product: '2', matno: '420200011480007', salebillno: 'xsdd2733-2', sales: '300'} {product: '5', matno: '420200011480008', salebillno: 'xsdd2738', sales: '600'} {product: '7', matno: '420200011480009', salebillno: 'xsdd2735', sales: '700'} {product: '6', matno: '420200011480010', salebillno: 'xsdd2734', sales: '400'}] /**降序**/ newArray.sort(sortByArr(['matno'],false)) // 输出 [{product: '6', matno: '420200011480010', salebillno: 'xsdd2734', sales: '400'} {product: '7', matno: '420200011480009', salebillno: 'xsdd2735', sales: '700'} {product: '5', matno: '420200011480008', salebillno: 'xsdd2738', sales: '600'} {product: '2', matno: '420200011480007', salebillno: 'xsdd2733-2', sales: '300'} {product: '1', matno: '420200011480006', salebillno: 'xsdd2733-8', sales: '800'} {product: '3', matno: '420200011480006', salebillno: 'xsdd2733-5', sales: '100'} {product: '4', matno: '420200011480006', salebillno: 'xsdd2733-3', sales: '200'} {product: '8', matno: '420200011480001', salebillno: 'xsdd2737', sales: '500'}]
以上就是“js中怎么对json数组进行排序”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。