您好,登錄后才能下訂單哦!
今天小編給大家分享一下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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。