Ghatzi
Ghatzi

Reputation: 597

Displaying dynamic data from object

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)}
)

Upvotes: 0

Views: 39

Answers (1)

Guerric P
Guerric P

Reputation: 31805

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

Upvotes: 1

Related Questions