简体   繁体   English

数组映射不返回值反应

[英]array mapping not returning value react

I have the following function 我有以下功能

  churnModel = () => {
        if (this.props.churnModel === undefined || this.props.churnModel.length === 0) {
          return("N/A")
        } else {
          this.props.churnModel.map((churn) => {
            if (churn === undefined || churn.length === 0) {
              return("N/A")
            } else {
              return(churn.map((model) => {
                this.service(model.scoreModelId)
              }))
            }
          })
        }
  };

The this.service functions looks like this... this.service函数看起来像这样...

service(e) {
  switch(e.toString().toLowerCase()) {
    case "in":
      return <span>in</span>
    case "rpt_callr":
      return <span>repeat</span>
    default:
      return <span>na</span>
  }
}

I am expecting to display the result in here: 我希望在这里显示结果:

<div className="riskScore">{this.churnModel()}</div>

Nothing gets displayed, but when I put in logs, those get printed. 什么都不会显示,但是当我输入日志时,这些都会被打印出来。

What is happening here? 这是怎么回事

you need to put return before this.props.churnModel.map . 您需要将return放在this.props.churnModel.map之前。 this.service(model.scoreModelId)

  1. A function will return undefined if nothing is nothing is returned. 如果什么也不返回, function将返回undefined
  2. map() takes a callback and changes each element of array to return value of the that callback. map()需要一个回调和改变数组的每个元素到return的该回调的值。 If you don't return anything all elements will be undefined 如果您不return任何内容,则所有元素均未undefined

You can also get rid of return before this.service(model.scoreModelId) by removing {} .Like this. 您也可以通过删除{}来摆脱this.service(model.scoreModelId)之前的return

return(churn.map((model) => this.service(model.scoreModelId)))

Here is the code 这是代码

churnModel = () => {
        if (this.props.churnModel === undefined || this.props.churnModel.length === 0) {
          return("N/A")
        } else {
          return this.props.churnModel.map((churn) => {
            if (churn === undefined || churn.length === 0) {
              return("N/A")
            } else {
              return(churn.map((model) => {
                return this.service(model.scoreModelId)
              }))
            }
          })
        }
  };

You have to use return statement in couple of lines: 您必须在几行中使用return语句:

churnModel = () => {
            if (this.props.churnModel === undefined || this.props.churnModel.length === 0) {
              return("N/A")
            } else {
              return this.props.churnModel.map((churn) => {
                if (churn === undefined || churn.length === 0) {
                  return("N/A")
                } else {
                  return(churn.map((model) => {
                    return this.service(model.scoreModelId)
                  }))
                }
              })
            }
      };

Why do you need return? 为什么需要退货? It's because you're using curly braces . 这是因为您使用的是花括号

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

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