按月订单排序数组

编程入门 行业动态 更新时间:2024-10-27 14:29:16
本文介绍了按月订单排序数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想对这个数组进行排序,根据月份 Jan,feb,march 等等。在前端使用JavaScript的

I want to sort this array, According to the month Jan, feb, march and so on. using JavaScript on front-end

[[["February",17],["January",30],["March",40],["April",40],["May",50],["June",60]]]

推荐答案

使用 sort() ,带有月订单映射对象

Use sort() with a month order mapping object

var data = [ ["June", 60], ["February", 17], ["January", 30], ["March", 40], ["April", 40], ["May", 50] ]; // object which holds the order value of the month var monthNames = { "January": 1, "February": 2, "March": 3, "April": 4, "May": 5, "June": 6, "July": 7, "August": 8, "September": 9, "October": 10, "November": 11, "December": 12 }; // sort the data array data.sort(function(a, b) { // sort based on the value in the monthNames object return monthNames[a[0]] - monthNames[b[0]]; }); document.write('<pre>' + JSON.stringify(data, 0, 3) + '</pre>')

更多推荐

按月订单排序数组

本文发布于:2023-08-02 15:21:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1278859.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   按月   订单

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!