2016-11-16 74 views
0

你好,我在JS新来的学生,我想用splitoutput[2]这样我就可以拿一个月的数量dd-mm-YYYY并使用pushnew array到EM保存新月的名字(str?)。JavaScript的使用拆分到新阵列阵列多维

例如:21/05/1989年,月的新数组中的一个新的名字:May

var output = [ 
 
    [ '0001', '0002', '0003', '0004' ], 
 
    [ 'Roman Alamsyah', 'Dika Sembiring', 'Winona', 'Bintang Senjaya' ], 
 
    [ '21/05/1989', '10/10/1992', '25/12/1965', '13/9/1994', '10/8/1994', '19/7/1994' ], 
 
] ; 
 

 
function mixarray(){ 
 

 
var months =[]; //probably wrong should just push to new array in output ? 
 
    //months.push([]); 
 
    //console.log(output[2].length); 
 

 
    for(var i = 0; i < output.length ;i ++){ 
 
    // console.log(output[i]); 
 
    
 
    for(var j=0 ; j< output[i].length; j++){ 
 
     months = output[i][j].split("/"); 
 
    } 
 
    } 
 
console.log(months); 
 
} 
 

 
mixarray(output);

我做了一些分,但不知何故我的大脑试图把EM后坠毁到新阵列并将其与月份名称(可能使用if-elsemonth's name吧?) 它可能会更好地推入new-array in output ,所以它会显示像这样以后与sorted(以后我可以做排序):

Months: 
August,Dec,July,May,Oct,Sept 

我只需要知道这个月怎么能推到一个新的数组:

['May','Oct','Dec','Sept','August','July']; 

从这个:

[ '21/05/1989', '10/10/1992', '25/12/1965', '13/9/1994', '10/8/1994', '19/7/1994' ] 
+0

当你使用'个月您将要覆盖在循环的每次迭代'months'的价值='。最终结果将是'months'与上一次迭代 – charlietfl

+0

中的拆分相同,您希望创建一个新月数组或用月份替换output [2]的内容? – brk

回答

0

JavaScript

// Need the names of the months tied to array to pull names 
 
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; 
 

 
// This is your input (not sure why it is named output) 
 
var output = [ 
 
    [ '0001', '0002', '0003', '0004' ], 
 
    [ 'Roman Alamsyah', 'Dika Sembiring', 'Winona', 'Bintang Senjaya' ], 
 
    [ '21/05/1989', '10/10/1992', '25/12/1965', '13/9/1994', '10/8/1994', '19/7/1994' ], 
 
]; 
 
    
 
// Converts DD/MM/YYYY to a JavaScript date object 
 
function toDate(dateStr) { 
 
    var parts = dateStr.split("/"); 
 
    return new Date(parts[2], parts[1] - 1, parts[0]); 
 
} 
 

 
// Pass in an array of date strings DD/MM/YYYY 
 
// Returns: An Array of Month Strings 
 
function mixArray(arr){ 
 
    var output = []; 
 
    for(var i = 0, len = arr.length; i < len; i++){ 
 
    output.push(monthNames[(toDate(arr[i])).getMonth()]); 
 
    } 
 
    return output; 
 
} 
 

 
console.log(mixArray(output[2]));

+0

thnx这样的细节和更正 –

0

你可以试试这个:

function mixarray(){ 

    var months =[]; 

    for(var i = 0; i < output.length ;i ++){ 
    // console.log(output[i]); 

    for(var j=0 ; j< output[i].length; j++){ 
     var parts = output[i][j].split("/"); 

     if (parts.length === 3) { 
      months.push(output[i][j].split("/")[1]) 
     } 
    } 
    } 
    console.log(months); 
} 
1

这里的概念很简单的证明:

var monthMap = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"]; 
 

 
// output [2] 
 
var fullArr = [ '21/05/1989', '10/10/1992', '25/12/1965', '13/9/1994', '10/8/1994', '19/7/1994' ]; 
 

 
var monthArray = fullArr.map(function(v) { 
 
    var numericMonth = Number(v.split('/')[1]); 
 
    
 
    return monthMap[numericMonth - 1]; 
 
}); 
 

 
console.log(monthArray); // ["May", "Oct", "Dec", "Sept", "Aug", "Jul"]

+0

这个解决方案的工作! –

0

应用与第二指标回路长度output array.And比较month数组的索引array.These的确切一个月。然后打印所有月份大举进军newmonths阵列

var output = [ 
 
     [ '0001', '0002', '0003', '0004' ], 
 
     [ 'Roman Alamsyah', 'Dika Sembiring', 'Winona', 'Bintang Senjaya' ], 
 
     [ '21/05/1989', '10/10/1992', '25/12/1965', '13/9/1994', '10/8/1994', '19/7/1994' ], 
 
    ] ; 
 
     var month=['jan' ,'feb','mar','aprl','may','jun','july','aug','sep','oct','nov','dec']; 
 
    function mixarray(){ 
 
     var newmonths =[]; 
 
     for(var j=0 ; j< output[2].length; j++){ 
 
       var mon = output[2][j].split('/'); 
 
       newmonths.push(month[mon[1]-1]) 
 
       } 
 
      console.log(newmonths) 
 
     } 
 
    mixarray(output);

1

如果您知道的日期将始终在output变量第三排,那么你可以跳过嵌套循环

const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; 
output = [[],[],[ '21/05/1989', '10/10/1992', '25/12/1965', '13/9/1994', '10/8/1994', '19/7/1994' ]] 
let months = [], 
    length = output[2].length; 
for (var i=0; i<length; i++) { 
    var elem = output[2][i]; 
    months.push(MONTHS[elem.split('/')[1]-1]); 
} 

console.log(months);