簡體   English   中英

在輸出帶引號的數組時出現問題

[英]Problems outputting arrays with quotation marks

我一直在嘗試獲取以下代碼,以在一個代碼中返回兩個獨立的數組。例如: [ 'July 1st', '4th' ] 但是,到目前為止,我的代碼返回: [ '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']);

您正在構建單個String並將其與Array文字包裝在一起。

從你所描述的東西,它看起來像你想建立多個項目這是你的數組變量。

例如

[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' ]

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

這樣使用forEach意味着您可以提供任意數量的日期-我的演示顯示3。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM