Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!