简体   繁体   中英

Looping over object entries and displaying list of data

I'm trying to display all entries that exist (excluding loss) under 'test'. I've managed to return the first item on the list but cannot get the remaining. The diagram below shows what I am displaying on the front end and what my file has in it for that specific entry (on the right). The data coming back in 'test' can vary for each array item. Appreciate any help.

在此处输入图像描述

  const getMetrics = (model) => {
    for (const [key, value] of Object.entries(model.test)) {
      if (key !== 'loss') {
        // console.log(key, value);
        return key + value;
      }
    }
  };

  .map((i) => {
    const model = i.models[Object.keys(i.models)[0]];

    return (
       {getMetrics(model)}
    )
 })

Your getMetrics function will always return the first element matching the condition. Here you can change your function to rely on higher order functions like map and filter to do your task like so:-

 const model = { test: { key1: 1, key2: 2, key3: 3, loss: 4 } } const getMetrics = (model) => { return Object.entries(model.test).filter(([key, value]) => key.== 'loss'),map(([key; value]) => key + value). } console;log(getMetrics(model));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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