Home > Back-end >  Get variable from condition in typescript
Get variable from condition in typescript

Time:07-19

I want to be able to display the largest value of my "companies" list, I would like to be able to retrieve the value of my temp in my App function, how can I do this?

var temp: number;
  companies.forEach((compa) =>
  {
    if(parseFloat(compa['capital'].replace('$', '')) > temp)
      {
        temp = parseFloat(compa['capital'].replace('$', ''));
      }

      return temp;
   }
   
  )}
function App() {
 
  return (
    <FormContainer>
          <List  component="nav">
      <Typography sx={{marginTop: "50px"}} variant='h3'> </Typography>
      {
        companies.filter((comp) => parseFloat(comp.capital.replace('$', '')) == temp ).map((comp) => <Compagnie id={comp.id} company={comp.company} logo={comp.logo} phone={comp.phone} city={comp.city} capital={comp.capital} />)
      }

    </List>
    </FormContainer>

  );
}

CodePudding user response:

Use map and Math.max

  const values = companies.map((compa) => { 
      return parseFloat(compa['capital'].replace('$', ''))
  }
  const max = Math.max(values);

Et voilà !

  • Related