Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

140
Vistas
How to search for a specific value in an array of objects

I want to make my code render the data when i search for a value, tried it by using for, but it keeps showing unexpected token. Is there any way to do it properly? It may be an easy question but i don't really know how to make this work.

export default function SelecionaRota() {
    const rota = {
        locais: [
            {
                nome: 'Rio de Janeiro',
                rotas: [
                    {
                        nome: 'RJ-SP',
                        valor: 1200
                    },
                    {
                        nome: 'RJ-BSB',
                        valor: 1400
                    }
                ]
            },
            {
                nome: 'São Paulo',
                rotas: [
                    {
                        nome: 'SP-RJ',
                        valor: 1200
                    },
                    {
                        nome: 'SP-BSB',
                        valor: 1400
                    }
                ]
            }
        ]
    }
    const location = useLocation()
    const { estadoOrigem } = location.state
    return (
        <div>
            <h1>{estadoOrigem}</h1>
            {for (var i = 0;i < rota.locais.length;i++) {
                if (estadoOrigem === rota.locais[i].nome) {
                    rota.locais[i].rotas.map(module => (
                        <>
                            <h1>{module.nome}</h1>
                            <h2>{module.valor}</h2>
                        </>
                    ))
                }
            }}
        </div>
    )
}

Error:

./src/pages/Rota.js
SyntaxError: D:\Arquivos\1 Estágio Teste\aeroporto-teste\src\pages\Rota.js: Unexpected token (39:13)

  37 |         <div>
  38 |             <h1>{estadoOrigem}</h1>
> 39 |             {for (var i = 0;i < rota.locais.length;i++) {
     |              ^
  40 |                 if (estadoOrigem === rota.locais[i].nome) {
  41 |                     rota.locais[i].rotas.map(module => (
  42 |                         <>
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

First find the item from list, then use map over rotas

Update: Adding the missing return statement.

return (
  <div>
    <h1>{estadoOrigem}</h1>
    {rota.locais
      .find((item) => estadoOrigem === item.nome)
      ?.rotas.map((module) => {
        return (
          <>
            <h1>{module.nome}</h1>
            <h2>{module.valor}</h2>
          </>
        );
      })}
  </div>
);
about 4 years ago · Juan Pablo Isaza Denunciar

0

you could do the 'filtering' or searching before the rendering.

const region = rota.locais.find(location => location.nome === estadoOrigem);

and then use the variable 'region' inside your return.

If you can have multiple regions, use:

const regions = rota.locais.filter(location => location.nome === estadoOrigem);
about 4 years ago · Juan Pablo Isaza Denunciar

0

I've never needed to use a for loop in the jsx of a component. Generally you'll want to have a component that does whatever filtering you want done in the javascript piece and then loop through the remaining data.

So I'd do:

let locations = []
const filterLocations = () => {
for (var i = 0; i < rota.locais.length; i++){
if(estadoOrigem === rota.locais[i].nome){
locations.push(i);
}
}}

Then the jsx would just do:

locations.map((i) => (( i.doStuff ))

You always want to start with the least efficient way of doing things and then trying to refactor little by little instead of trying to figure out something clever right off the bat. Like instead of doing that for loop.

You could also use the javascript .filter() method to filter out whatever data you need and then push that to an array to map through.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda