S
S
Stepan2019-12-22 14:48:48
JavaScript
Stepan, 2019-12-22 14:48:48

How to generate an object from an array?

There is the following object:

{
    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'}
      ]
    ]
  };

But I want to generate it from separate variables:
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"}']

For this I want to use the function:
keboard_maker()

But I don’t understand how to combine this in a cycle (Tell me how to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2019-12-22
@Stepashka20

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question