Stepashka20
@Stepashka20
(☞゚ヮ゚)☞

Как сгенерировать объект из массива?

Есть следующий объект:
{
    one_time: false,
    buttons: [
      [
        {action: {type: 'text',label: 'Профиль1',payload: '{"command": "profile1"}' }, color: 'default'}
      ],
      [
         {action: {type: 'text',label: 'Профиль2',payload: '{"command": "profile2"}' }, color: 'default'},
         {action: {type: 'text',label: 'Профиль3',payload: '{"command": "profile3"}' }, color: 'default'},
         {action: {type: 'text',label: 'Профиль4',payload: '{"command": "profile4"}' }, color: 'default'}
      ]
    ]
  };

Но я хочу генерировать его из отдельным переменных:
var button1=["Профиль1",'default','{"command": "profile1"}'] 
//{action: {type: 'text',label: 'Профиль1',payload: '{"command": "profile1"}' }, color: 'default'}
var button2=["Профиль2",'default','{"command": "profile2"}']
var button3=["Профиль3",'default','{"command": "profile3"}']
var button4=["Профиль4",'default','{"command": "profile4"}']

Для этого хочу использовать функцию:
keboard_maker([[button1,button2],[button3],[button4]])

Но как это в цикле совместить не понимаю( Подскажите,как это сделать?
  • Вопрос задан
  • 94 просмотра
Решения вопроса 1
hzzzzl
@hzzzzl
var button1=["Профиль1",'default','{"command": "profile1"}'] 
var button2=["Профиль2",'default','{"command": "profile2"}']
var button3=["Профиль3",'default','{"command": "profile3"}']
var button4=["Профиль4",'default','{"command": "profile4"}']

btns = [button1,button2,button3,button4]

obj = {
    one_time: false,
    buttons: btns.map(btn => ({
        action: {
            type: 'text',
            label: btn[0],
            payload: btn[2]
        },
        color: btn[1]
    }))
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы