Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Помощь с массивом (https://javascript.ru/forum/misc/80735-pomoshh-s-massivom.html)

AleBa 22.07.2020 23:15

Помощь с массивом
 
Добрый день.
Подскажите пжл как написать функцию, чтобы значение Weekends('long') = const long, а Weekends('short') = const short?

const long = ['saturday', 'sunday'];
const short = ['sat', 'sun'];

const weekends = () => {

}

weekends('long')


Сделал сам:
const long = ['saturday', 'sunday'];
const short = ['sat', 'sun'];

export const getWeekends = (number) => {
  switch (number) {
    case long:
      return ['saturday', 'sunday'];
    case short:
      return ['sat', 'sun'];
    case 'short':
      return ['sat', 'sun'];
      default:
      return ['saturday', 'sunday'];
  }
};

laimas 23.07.2020 05:04

Читать https://developer.mozilla.org/ru/doc...tements/switch

рони 23.07.2020 07:58

Цитата:

Сообщение от AleBa
как написать функцию, чтобы значение Weekends('long') = const long, а Weekends('short') = const short?

const long = ['saturday', 'sunday'];
const short = ['sat', 'sun'];
const weekends = key => ({long, short}[key]);
alert(weekends('long'));

AleBa 24.07.2020 18:16

const long = ['saturday', 'sunday'];
const short = ['sat', 'sun'];
const weekends = key => ({long, short}[key])


А как сделать здесь значение default? Например при вызове weekends(), возвращает ['saturday', 'sunday'].

Спасибо!

Nexus 24.07.2020 19:31

Цитата:

Сообщение от AleBa
А как сделать здесь значение default? Например при вызове weekends(), возвращает ['saturday', 'sunday'].

const weekends = key => ({long, short}[key || 'long']);


Часовой пояс GMT +3, время: 15:42.