Javascript 输出带引号的数组时出现问题
我一直在尝试让下面的代码在一个数组中返回两个separate数组。。例如:Javascript 输出带引号的数组时出现问题,javascript,arrays,Javascript,Arrays,我一直在尝试让下面的代码在一个数组中返回两个separate数组。。例如:[“7月1日”、“4日]。但是,到目前为止,我的代码返回:['July 1st,4th']。注意,我需要它有准确的引号 这是我的代码: function friendly(str) { var final = []; var months = {01:"January" , 02: "February", 03:"March", 04:"April", 05:"May", 06:"June", 07:"July",
[“7月1日”、“4日]
。但是,到目前为止,我的代码返回:['July 1st,4th']
。注意,我需要它有准确的引号
这是我的代码:
function friendly(str) {
var final = [];
var months = {01:"January" , 02: "February", 03:"March", 04:"April", 05:"May", 06:"June", 07:"July", 08:"August", 09:"September", 10:"October", 11:"November", 12:"December"}, i, c, date1, date2, getYear1, getMonth1, getDay1, getYear2, getMonth2, getDay2;
var days = {01: "st", 02: "nd", 03: "rd", 04: "th", 05: "th"};
date1 = str[0];
date1.split('-');
date2 = str[1];
date2.split('-');
getYear1 = date1.substring(0, 4);
getMonth1 = date1.substring(5, 7);
getMonth2 = date2.substring(5, 7);
getDay1 = date1.substring(8, 10);
getYear2 = date2.substring(0,4);
getMonth2 = date2.substring(5,7);
getDay2 = date2.substring(8, 10);
for(var key in months){
//console.log(getMonth1.charAt(0) == 0);
if(getMonth1.charAt(0) == 0){
getMonth1 = getMonth1.slice(1);
}
if(getMonth2.charAt(0) == 0){
getMonth2 = getMonth2.slice(1);
}
if(getDay1.charAt(0) == 0){
getDay1 = getDay1.slice(1);
}
if(getDay2.charAt(0) == 0){
getDay2 = getDay2.slice(1);
}
if(days.hasOwnProperty(getDay1)){
getDay1 = getDay1 + days[getDay1];
}
if(days.hasOwnProperty(getDay2)){
getDay2 = getDay2 + days[getDay2];
}
if(months.hasOwnProperty(getMonth1)){
getMonth1 = months[getMonth1];
}
if(months.hasOwnProperty(getMonth2)){
getMonth2 = months[getMonth2];
}
if(getMonth1 == getMonth2 && getYear1 == getYear2 && getDay1 !== getDay2){
return [getMonth1 + ' ' + getDay1 + ', ' + getDay2.split(',')];
//console.log(getMonth1);
}
else if(getMonth1 == getMonth2 && getYear1 == getYear2 && getDay1 == getDay2){
return [getMonth1 + ' ' + getDay1 + ', ' + getYear1];
}
else if(getYear1 !== getYear2 && getMonth1 !== getMonth2 && getDay1 !== getDay2){
return [getMonth1 + ' ' + getDay1 + ', ' + getMonth2 + ' '+ getDay2.split(',')];
}
else if(getYear1 == getYear2 && getMonth1 !== getMonth2 && getDay1 !== getDay2){
return [getMonth1 + ' ' + getDay1 + ', ' + getMonth2 + ' '+ getDay2 + ', ' + getYear1];
}
else if(getYear1 == getYear2 && getMonth1 !== getMonth2 && getDay1 !== getDay2){
return;
}
else if (getYear1 !== getYear2 && getMonth1 == getMonth2 && getDay1 !== getDay2){
return [getDay1 + ', ' + getDay2.split(',')];
}
}
}
friendly(['2015-07-01', '2015-07-04']);
您正在构建一个字符串,并用数组文本将其包装
根据您所描述的,看起来您想要构建一个包含多个项目的数组,这些项目是您的变量
比如说
[getMonth1 + ' ' + getDay1 + ', ' + getDay2.split(',')];
// ^^^^^^^^^^ ^^^^^^^^^^^^^
// becomes
[getMonth1 + ' ' + getDay1, getDay2];
// ^^ ^
// or possibly
[getMonth1 + ' ' + getDay1].concat(getDay2.split(','));
你已经发布了很多代码,实际上很少涉及到你的问题,当你试着在将来调试时,考虑你的变量在每一行上,然后你应该能够把你的问题固定或缩小到一个简单的例子,这对于每个人来说都是更容易理解的,例如
var foo = "July", // it doesn't matter how we arrived at these variables
bar = "1st", // all that will matter for the question is how they
baz = "4th"; // are used from here onwards
[foo + ' ' + bar + ', ' + baz]; // unexpected result
[foo + ' ' + bar, baz]; // expected result
还有很多方法可以用更少的代码来实现这一点。这里有一个快速的例子来启发你
function friendly(arrDates) {
var suffixes = ["", "st", "nd", "rd"];
var months = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var result = [];
arrDates.forEach(function(date) {
var parts = date.split("-");
result.push(
months[parseInt(parts[1])] +
" " +
(parts[2].slice(0,1) == 0 ? parts[2].slice(1) : parts[2]) +
(suffixes[parseInt(parts[2])] || "th")
);
});
return result;
}
result = friendly(['2015-07-01', '2015-07-04', '2015-09-16']);
console.log(result);
这将打印出:
[ 'July 1st', 'July 4th', 'September 16th' ]
例如
像这样使用
forEach
意味着您可以提供任意数量的日期-我的演示显示了3个。废话!谢谢你注意到这个lol,不过还是有我的问题。谢谢你也告诉我我做错了什么。哇!太好了,谢谢你给我看这个。这非常有帮助!