Почему не работает reduce по маске?

Не заменяет букву h, в чем может быть ошибка?

11.12.2021 h:17:54

но при split = false заменяет

function dt(split){
  let now = new Date(1639171074046);
  let data = {
    d: split ? now.getDate() : ('0'+ now.getDate()).slice(-2),
    m: ('0'+ (now.getMonth() + 1)).slice(-2),
    y: now.getFullYear(),
    h: split ? now.getHours() : ('0'+ now.getHours()).slice(-2),
    i: ('0'+ now.getMinutes()).slice(-2),
    s: ('0'+ now.getSeconds()).slice(-2)
  };
  console.log(data);
  return [...'d.m.y h:i:s'].reduce((res, el) => {
    return res += data[el] || el;
  }, '');
}

console.log( dt(true) );
console.log( dt(false) );


Ответы (1 шт):

Автор решения: Igor

У Вас ноль часов. Ноль в булевском смысле - false:

data[el] || el; // returns el if data[el] is zero
→ Ссылка