Here is the code snippet I am working with:
bodyLength.forEach((el, i) => {
console.log(`${values.bodyTitleEn2 + i.toString()}`);
body.push({
title: [
{
key: 'en',
value: values.bodyTitleEn + i,
},
{
key: 'ku',
value: values.bodyTitleKu + i,
},
{
key: 'ar',
value: values.bodyTitleAr + i,
},
] ...
The variables values.bodyTitleEn
, values.bodyTitleKu
, and values.bodyTitleAr
have numbers at the end of them (such as values.bodyTitleEn0
, values.bodyTitleEn1
, values.bodyTitleEn2
). I also have an array called bodyLength
which contains the number of bodyTitle[En]
elements. I am trying to dynamically add the number at the end using values.bodyTitleEn + i
, but it throws an error saying values.bodyTitleEn
is undefined. This is because bodyTitleEn
does not exist in the object without the number at the end. How can I go about achieving this? Thank you.