gpt4 book ai didi

javascript - 将数组元素从一种形式格式化为另一种形式-JS

转载 作者:行者123 更新时间:2023-11-30 07:49:04 27 4
gpt4 key购买 nike

我有一个字符串数组,它们实际上是 201001 ,201002. 形式的日期。我想将这些数组项转换为类似 2010 January, 2010 February 的格式。有什么方法可以做到这一点。

var Array = ["201005", "201006", "201007", "201008", "201009", "201010", "201011", "201012", "201101", "201102", "201103", "201104", "201106", "201107", "201108", "201109", "201110", "201111", "201112", "201201", "201202", "201203", "201204", "201205", "201206", "201207", "201208", "201209", "201210", "201211", "201212", "201301", "201302", "201303", "201304", "201305", "201306", "201307"];

我正在寻找像这样的数组:

var expected = ["2010 january", "2010 February" etc]

最佳答案

你可以这样做:

const monthMap = {
"01": "January",
"02": "February",
"03": "March",
"04": "April",
"05": "May",
"06": "June",
"07": "July",
"08": "August",
"09": "September",
"10": "October",
"11": "November",
"12": "December"
};

xAxisArray = xAxisArray.map(axis => {
const year = axis.substring(0, 4);
const month = axis.substring(4, 6);

return `${year} ${monthMap[month]}`;
});

或者您可以使用 moment ,但这可能有点矫枉过正。

关于javascript - 将数组元素从一种形式格式化为另一种形式-JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57607456/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com