访问React中对象的动态属性
我正在检查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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码有多个问题。您不能只使用
$ {lang}
。如果要使用模板文字)要访问对象的属性,您需要AA键,即您已经拥有的字符串,因此在这种情况下,根本不需要文字。
当您通过索引访问数组时,您需要使用
[]
不是()
,请使用delmethod [i]
而不是delmethod( i)
。另外,请确保属性” 。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 usedelmethod[i]
instead ofdelmethod(i)
. Additionally make sure an property exists on an JavaScript object.