简体   繁体   English

基于字符串连接从对象中获取值

[英]Get value from object based on string concatenation

I have a small website written with React & Typescript which works with a Spring boot backend.我有一个用 React & Typescript 编写的小网站,它与 Spring 启动后端一起使用。 I'm now trying to implement multiple languages on it but I am hitting an issue.我现在正在尝试在其上实现多种语言,但遇到了一个问题。

Before the implementation of languages the issue wasn't present because I could just get the value based on it's name:在实现语言之前,这个问题不存在,因为我可以根据它的名称获取值:

<div className="product-category-list">
  {props.productCategory.map(function (item, id) {
    return <div className="product-category-div">
      <img className="product-category-image" src={"data:image/png;base64, " + item.picture}/>
      <p className="product-category-title">{ item.name }</p>
    </div>
  })}
</div>

This results in the value of item.name being shown on the website the way it should be.这会导致item.name的值以item.name的方式显示在网站上。

Now the code is different because the name property of item has been changed to multiple values (nameEN, nameFR, nameNL, ...).现在代码不同了,因为 item 的 name 属性已更改为多个值(nameEN、nameFR、nameNL、...)。 To implement this the language is saved on the application and can get called.为了实现这一点,语言被保存在应用程序中并且可以被调用。 So now the code is as follows:所以现在代码如下:

<div className="product-category-list">
  {props.productCategory.map(function (item, id) {
    return <div className="product-category-div">
      <img className="product-category-image" src={"data:image/png;base64, " + item.picture}/>
      <p className="product-category-title">{ "item.name".concat(getLanguage().toUpperCase()) }</p>
    </div>
  })}
</div>

Because of this change the website shows the String item.nameEN or item.nameNL in the frontend instead of the value.由于此更改,网站在前端显示字符串item.nameENitem.nameNL而不是值。 I've tried several ways of fixing it and researched it but I can't find any information on this specific issue.我已经尝试了多种修复方法并对其进行了研究,但我找不到有关此特定问题的任何信息。

I'm looking for a way to be able to get the value of an item key which is build based on a String concatenation.我正在寻找一种能够获取基于字符串连接构建的项目键值的方法。 Any help would be greatly appreciated because I'm very stuck.任何帮助将不胜感激,因为我很困。

Thank you!谢谢!

Let's take a look at your previous code which you said was working:让我们看一下您之前所说的有效代码:

<p className="product-category-title">{ item.name }</p>

Here you are accessing a property name of the object item .在这里,您正在访问对象item的属性name

In your new code:在您的新代码中:

<p className="product-category-title">{ "item.name".concat(getLanguage().toUpperCase()) }</p>

Here you are just returning a string with the value "item.nameEN" and not accessing the object called item .在这里,您只是返回一个值为"item.nameEN"的字符串,而不是访问名为item的对象。

What you want is to still access item but do that with a key that you are building dynamically.您想要的是仍然访问item但使用您正在动态构建的密钥来执行此操作。 Try this:尝试这个:

<p className="product-category-title">{ item["name" + getLanguage().toUpperCase()] }</p>

just replacing只是更换

"item.name".concat(getLanguage().toUpperCase())

with

item["name" + getLanguage().toUpperCase()

should work fine应该工作正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM