简体   繁体   English

在输出带引号的数组时出现问题

[英]Problems outputting arrays with quotation marks

I've been trying to get the following code to return two sepparate arrays within one.. for example : [ 'July 1st', '4th' ] . 我一直在尝试获取以下代码,以在一个代码中返回两个独立的数组。例如: [ 'July 1st', '4th' ] However, so far my code returns : [ 'July 1st, 4th' ] . 但是,到目前为止,我的代码返回: [ 'July 1st, 4th' ] Note, I need it to have the exact quotation marks. 请注意,我需要它具有确切的引号。

This is my code: 这是我的代码:

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']);

You're building a single String and wrapping it with an Array literal . 您正在构建单个String并将其与Array文字包装在一起。

From what you've described, it looks like you want to build an Array of multiple items which are your variables. 从你所描述的东西,它看起来像你想建立多个项目这是你的数组变量。

For example 例如

[getMonth1 + ' ' + getDay1 + ', ' + getDay2.split(',')];
//                        ^^^^^^^^^^       ^^^^^^^^^^^^^
// becomes
[getMonth1 + ' ' + getDay1, getDay2];
//                        ^^        ^
// or possibly
[getMonth1 + ' ' + getDay1].concat(getDay2.split(','));

You have posted a lot of code and very little actually pertains to your issue, when you try to debug in future consider what your variables are on each line and then you should be able to either fix or narrow down your problem to a simple example which will be much easier to understand for everyone, eg 您已经发布了很多代码,而实际上与问题无关,当您以后尝试调试时,请考虑每行上的变量,然后应该可以将问题解决或缩小为一个简单的示例,每个人都会更容易理解,例如

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

There's also any number of ways you can so this with a lot less code. 您还可以通过多种方法来使用更少的代码。 Here's a quick one to inspire you! 这是一个可以激发您灵感的快速方法!

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);

this prints out: 打印出来:

[ 'July 1st', 'July 4th', 'September 16th' ]

Eg - http://repl.it/xip 例如-http: //repl.it/xip

Using forEach like this means you can supply any number of dates - my demo shows 3. 这样使用forEach意味着您可以提供任意数量的日期-我的演示显示3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM