Dell Watson
Dell Watson

Reputation: 367

javascript array multidimensional using split to a new array

Hello I'm new student in js, I'm trying to use split in output[2] so i can take the Month's number dd-mm-YYYY and using push to a new array to save em with a new name of months (str ?) .

ex: 21/05/1989 with a new name of month in a new array : 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);

I did some split but somehow my brain crashed after trying to push em to new array and combine it with month's name(probably using if-else for month's name huh ?) it might better to push in new-array in output so it will show like this later with sorted ( i can do the sort later ) :

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

I just need to know how this months be able to push into a new array :

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

from this :

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

Upvotes: 0

Views: 334

Answers (5)

Ben
Ben

Reputation: 1568

Here's a very simple proof of concept:

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"]

Upvotes: 1

prasanth
prasanth

Reputation: 22500

Apply the for loop length with second index of the output array.And compare the index with month array .Then print the exact month of the array.These all the month push into newmonths array

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

Upvotes: 0

sid-m
sid-m

Reputation: 1554

If you know that the dates will always be in the third array in the output variable, then you can skip the nested loop

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

Upvotes: 1

Dhananjaya Kuppu
Dhananjaya Kuppu

Reputation: 1322

You may try this:

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

Upvotes: 0

abc123
abc123

Reputation: 18783

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

Upvotes: 0

Related Questions