How to execute a loop in a dedent block?

Viewed 28

Does anyone know how I can execute

req.body.documents.forEach(element => {
    console.log(element['_index']);
    console.log(element['_id']);
    console.log(element['_source'].uuid);
    console.log('--------------------');
});

inside a dedent block?

const description = dedent`

Date:     ${req.body.date}
Alert ID: ${req.body.alert_id}
    
LOG ENTRIES
===========

< Execute loop here >
`;

Or does this need to be solved with a template engine? If, any suggestions how to implement it?

1 Answers

Remember, you can interpolate even complex expressions inside template literals:

`LOG ENTRIES
===========

${
  req.body.documents.map(element => 
    [element._index, element._id, element._source.uuid].join('\n')
  ).join('--------------------\n')
}

`

Then dedent will just take care of extra indentation in the resulting string.

Related