简体   繁体   English

ReactJS-如何在API中获取值以显示文本?

[英]ReactJS - How to get a value in an API to display text?

My JSON API displays the following: 我的JSON API显示以下内容:

 "crgAudittype": {
            "$id": "4",
            "Value": 3
        },

What I'm attempting to do is have it to display the word "transition" in place of "3". 我正在尝试做的是用它显示单词“ transition”而不是“ 3”。 So I created a ".js" file with the following code. 因此,我使用以下代码创建了一个“ .js”文件。

export const AUDIT_TYPES = {
    OCCUPIED: 'OCCUPIED',
    UNOCCUPIED: 'UNOCCUPIED',
    TRANSITION: 'TRANSITION',
    SHUTDOWN: 'SHUTDOWN'
};

export const allAuditTypes = [AUDIT_TYPES.OCCUPIED, AUDIT_TYPES.UNOCCUPIED, AUDIT_TYPES.TRANSITION, AUDIT_TYPES.SHUTDOWN];

export const auditTypeToIdMap = {
    1: AUDIT_TYPES.OCCUPIED,
    2: AUDIT_TYPES.UNOCCUPIED,
    3: AUDIT_TYPES.TRANSITION,
    4: AUDIT_TYPES.SHUTDOWN
};

export const getAuditTypeDescFromId = (auditTypeId) => auditTypeToIdMap[Number(auditTypeId)];

...I declared the ".js" file in the header of the page I want to employ it in. ...我在要使用它的页面的标题中声明了“ .js”文件。

import { getAuditTypeDescFromId, allAuditTypes, auditTypeToIdMap } from '../constants/audit-types';

now in the Render() section, I assigned the value like so: 现在在Render()部分,我按如下所示分配值:

<b>Audit type:</b>  {Object.keys(auditTypeToIdMap).map(id => { id })}<br />

...however, I'm not sure syntax-wise, How to set the text to display in place of the value using the above code. ...但是,我不确定从语法角度来说,如何使用上述代码将文本设置为代替值显示。 Could I get some help with this please? 请给我一些帮助吗? ...Thank you in advance ...先感谢您

You can do something like this: 您可以执行以下操作:

const response = {
    "crgAudittype": {
      "$id": "4",
      "Value": 3
     },
}

const auditTypes = {
    "1": "OCCUPIED",
    "2": "UNOCCUPIED",
    "3": "TRANSITION",
    "4": "SHUTDOWN"
};

class App extends Component {
  constructor() {
    super();
  }

  render() {
    return (
      <div>
        Audit Type: {Object.entries(auditTypes).filter(([key, value]) => {
          return key === response.crgAudittype.Value.toString();
        }).map(([key, value]) => {
          return value 
        })}
      </div>
    );
  }
}

Tough because we don't know the structure of auditTypeToIdMap looks like but I think I see your problem. 严格,因为我们不知道auditTypeToIdMap的结构是什么样,但我认为我看到了您的问题。 You're using Object.keys and map, which means you're essentially mapping through your array twice. 您正在使用Object.keys和map,这意味着您实际上是两次遍历数组。 Once with Object.keys, and then within that iteration you're mapping through all of the answers again with map(), thereby giving you four "labels". 一旦使用Object.keys,然后在该迭代中,您将再次使用map()映射所有答案,从而为您提供四个“标签”。

Also, since you've already written your function above, this should work if you replace it in your render assuming everything is correct leading up to that point and you've imported everything correctly. 另外,由于您已经在上面编写了函数,因此,如果假设到那时为止一切正确,并且已经正确导入了所有内容,则在渲染器中替换它就可以了。

<b>Audit type:</b>  {this.auditTypeDescFromId(id)}

assuming the auditTypeDescFromId function is in the same file as your render 假设auditTypeDescFromId函数与渲染器在同一文件中

I do think there's a cleaner way to handle this however, perhaps using filter(). 我确实认为有一种更干净的方法可以解决此问题,也许可以使用filter()。 If you can explain what data you have and what you're trying to do with it we could help you more. 如果您可以解释您拥有的数据以及您打算使用的数据,我们将为您提供更多帮助。

const response = {
   "crgAudittype": {
      "$id": "4",
      "Value": 3
   }
};

const auditTypes = {
   "1": "OCCUPIED",
   "2": "UNOCCUPIED",
   "3": "TRANSITION",
   "4": "SHUTDOWN"
};

const dom = <b>Audit type:</b> {auditTypes[response.crgAudittype.Value]}<br />

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

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