0

I want to use a dynamic name variable inside a loop but it shows the error "EmbedMessage is not defined". Is there a way to do it?

const EmbedMessage1 = {
 title: '__TITLE1__',
 description: '**First embed message**',
 //etc.
};
const EmbedMessage2 = {
 title: '__TITLE2__',
 description: '**Second embed message**',
 //etc.
};
//etc.

for (let i = 1; i < 4; i++) {
 message.channel.send({ embed: EmbedMessage[i] }).then((msg) => {
  //function
 });
}

1 Answer 1

2

You should use an array of embeds instead of naming your variables like that.

const embeds = [
 {
  title: '__TITLE1__',
  description: '**First embed message**',
  //etc.
 },
 {
  title: '__TITLE2__',
  description: '**Second embed message**',
  //etc.
 },
];
//etc.

for (let i = 1; i < 4; i++) {
 message.channel.send({ embed: embeds[i] }).then((msg) => {
  //function
 });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks ! Changing my data format to iterate, nice idea!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.