访问React中对象的动态属性

发布于 2025-01-21 08:03:53 字数 647 浏览 0 评论 0原文

我正在检查AlertDetails对象中的“全”字符串中是否存在占位符在其中,用户可以选择通过不同的交付方法发送警报),因此我将所有可能的交付方法保存在数组中,并像以下一样尝试;

Const delmethod=[“email”,”sms”,”fax”]

for(let i=0;i<delmethod.length;i++)
{
Console.log(alertDetails &&
            alertDetails.alertMessage &&
            alertDetails.alertMessage[${lang}] &&
            alertDetails.alertMessage[${lang}].all
            ? alertDetails.alertMessage[${lang}][‘${delmethod(i)}’].includes('placeholder')
            : false;
   }

PS:属性“全部”已修复,只是电子邮件传真将根据用户的输入进行更改,以总结一下,如果在任何字符串中都存在“占位符”(ALL,Email,Email,Email,Fax,SMS),我想返回true。我尝试的方法只打印了Del方法阵列,感谢您的任何帮助!

I’m checking whether there is a placeholder present in the “all” string within alertDetails object so my question is I need to access email,sms,fax property that appears dynamically based on user’s input (the code is a part of an alert box in which an user can choose to send alert via different delivery methods) so I saved all the possible delivery methods in an array and tried like below;

Const delmethod=[“email”,”sms”,”fax”]

for(let i=0;i<delmethod.length;i++)
{
Console.log(alertDetails &&
            alertDetails.alertMessage &&
            alertDetails.alertMessage[${lang}] &&
            alertDetails.alertMessage[${lang}].all
            ? alertDetails.alertMessage[${lang}][‘${delmethod(i)}’].includes('placeholder')
            : false;
   }

P.S:the property “all” is fixed it’s just the email fax will be changing based on user’s input, to sum it up I want to return true if “placeholder” exists in any of the strings(all,email,fax,sms) the method I tried just prints the del method array, I’d appreciate any help thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜味拾荒者 2025-01-28 08:03:53

您的代码有多个问题。您不能只使用$ {lang}。如果要使用模板文字

要访问对象的属性,您需要AA键,即您已经拥有的字符串,因此在这种情况下,根本不需要文字。

当您通过索引访问数组时,您需要使用[]不是(),请使用delmethod [i]而不是delmethod( i)。另外,请确保属性” 。

const delmethod = ["email", "sms", "fax"];

const alertDetails = {
  alertMessage: {
    en: {
      all: "emailsfsdfsdfsd",
      fax: "placeholder",
      sms: "sdkjföskjfsödkj"
    },
  },
};

const lang = "en";

for (let i = 0; i < delmethod.length; i++) {
  if (
    alertDetails &&
    alertDetails.alertMessage &&
    // use backticks ` when trying to use template literals
    alertDetails.alertMessage[`${lang}`] &&
    // there is actually no need for template literals here
    alertDetails.alertMessage[lang].all &&
    // you need to make sure "sms" or "fax" or "email" key actually exist on the object
    alertDetails.alertMessage[lang][delmethod[i]] &&
    alertDetails.alertMessage[lang][delmethod[i]].includes("placeholder")
  ) {
    console.log(
      `alertDetails.alertMessage[${lang}][${delmethod[i]}] does have a placeholder`
    );
    console.log(true);
  } else {
    console.log(
      `alertDetails.alertMessage[${lang}] does NOT have property ${delmethod[i]} or does NOT have a placeholder`
    );
    console.log(false);
  }
}

There are multiple issues with your code. You cannot just use ${lang}. You must surround your string with backticks (`) if you want to use template literals.

To access properties of an object you need a a key i.e. a string which you already have so in this case template literals are not required at all.

When you access an array by index you need to use [] not () so use delmethod[i] instead of delmethod(i). Additionally make sure an property exists on an JavaScript object.

const delmethod = ["email", "sms", "fax"];

const alertDetails = {
  alertMessage: {
    en: {
      all: "emailsfsdfsdfsd",
      fax: "placeholder",
      sms: "sdkjföskjfsödkj"
    },
  },
};

const lang = "en";

for (let i = 0; i < delmethod.length; i++) {
  if (
    alertDetails &&
    alertDetails.alertMessage &&
    // use backticks ` when trying to use template literals
    alertDetails.alertMessage[`${lang}`] &&
    // there is actually no need for template literals here
    alertDetails.alertMessage[lang].all &&
    // you need to make sure "sms" or "fax" or "email" key actually exist on the object
    alertDetails.alertMessage[lang][delmethod[i]] &&
    alertDetails.alertMessage[lang][delmethod[i]].includes("placeholder")
  ) {
    console.log(
      `alertDetails.alertMessage[${lang}][${delmethod[i]}] does have a placeholder`
    );
    console.log(true);
  } else {
    console.log(
      `alertDetails.alertMessage[${lang}] does NOT have property ${delmethod[i]} or does NOT have a placeholder`
    );
    console.log(false);
  }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文