0

I'm trying to display the 'helpText' data on the front end depending on the type. I's it possible to pass a conditional statement (metricsType variable) and the [key] value into what I'm trying to return. The last 'p' below gives you an idea of what I'm trying to achieve.

The key values are binary_accuracy or rmse in this example (from json file).

Appreciate any help.

const getMetrics = (model, type) => {
    const metricsType = type === 'REGRESSION' ? 'regression' : 'binary';

    const metrics = {
      binary: {
        binary_accuracy: {
          helpText: 'helptext 1',
        },
      },
      regression: {
        rmse: {
          helpText: 'helptext 2',
        },
      },
    };

    return Object.entries(model.test)
      .filter(([key]) => key !== 'loss')
      .map(([key, value]) => (
        <div>
          <div>
            <h4>{key}</h4>
   
            <p>{metrics.binary.binary_accuracy.helpText}</p>
            // output: helptext 1

            <p>{metrics.regression.rmse.helpText}</p>
            // output: helptext 2

            <p>{metrics.{metricsType}.[key].helpText}</p>
            // this does not work but an idea of what I'm trying to do. I've tried backticks, ${}, + but no luck.

          </div>
        </div>
      ));
  };

--------

return (
  {getMetrics(model, i.type)}
)
0

1 Answer 1

1

What you need is probably {metrics[metricsType][key].helpText} (hard to say without knowing what's inside model).

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your help. I was getting an 'helpText is undefined error' so I ended up getting it to work like this. {metrics[metricsType][key] && metrics[metricsType][key].helpText}. Not sure if theirs a better way.
yes it's a nice solution, depending on the browsers you target and the bundle tooling you use, you could also write this {metrics[metricsType][key]?.helpText}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.