Home > Software engineering >  Combine 2 arrays of different lengths and sort date
Combine 2 arrays of different lengths and sort date

Time:11-02

I have 2 arrays with different lengths

let daysArray = ['09-20', '09-21', '09-22', '09-23', '09-24', '09-25', '09-26']

let weekNameArray = ['Mi', 'Do', 'Fr', 'Sa So', 'Mo', 'Di' ]

combined them with _.zipWith:

let weakDateArr = _.zipWith(weekNameArray, daysArray , function(first, second) {
          return first   " "   second  
        });

How I can combine them to get the next array:

['Mi 09-20', 'Do 09-21', 'Fr 09-22', 'Sa So', 'Mo 09-25', 'Di 09-26']

'Sa So' must be undated *

CodePudding user response:

Hmm, not sure lodash has support for this out of box...

You could apply a simple custom solution using Array.map() and an index offset for the daysArray:

days_offset = 0;
let combined = weekNameArray.map( (name, index) => {
    if(name.length <= 2) {
    return name   ' '   daysArray[ index - days_offset ];
  } else {
    days_offset  = 1;
    return name;
  }
});
  • Related