javascriptarraylistaws-lambdaslack-commandsslack-block-kit

How to populate my checkbox in my view.js from an array in my controller.js? (Block Kit Slack)


How do I populate this checkbox with the data from an array in my controller?

My-view.js

function arrayBlock() {
    return createModalBlock('Engage', [
        {
            type: 'section',
            text: {
                type: 'mrkdwn',
                text: 'This is a section block with checkboxes.'
            },
            accessory: {
                type: 'checkboxes',
                options: [
                    {
                        text: {
                            type: 'mrkdwn',
                            text: 'Option 1'
                        },
                        description: {
                            type: 'mrkdwn',
                            text: 'user description'
                        },
                        value: 'value-0'
                    }
                ],
                action_id: 'checkboxes-action'
            }
        }
    ], {
        close: {
            type: 'plain_text',
            text: 'Close',
        },
    });
}

controller.js

arr = ["option 1", "option 2", "option 3", "option 4", "option 5"]

Checkbox options should be option 1, option 2, [...] enter image description here


Solution

  • I can't copy your code, since you posted it as a Image, but this is how you can do it:

    arr = ["option 1", "option 2", "option 3", "option 4", "option 5"]
    
    let arrayResult = arr.map((option, optionID) => {
        return {
            "text": {
                "type": "mrkdwn",
                "text": option
            },
            "description": {
                "type": "mrkdwn",
                "text": optionID
            },
            "value": `value-${optionID}`
        }
    })
    
    console.log(arrayResult)

    Using arr.map() will solve your problem.