Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

101
Visualizações
ReactJS. How to sort through data and find equal value of name property to id and store the object in a variable

I have a database and I am trying to sort through the data in the database and find the one that has a name property equal to the id variable that I specified. The id variable works though I am having trouble sorting through and finding the name property equal to the id variable. This what I have tried:

import {useParams} from "react-router-dom";

function StudentInfo() {
    
    const {id} = useParams();
    var studentData = {};

    function infoGetter() {
    
        fetch(
            "{url that I put in real code}"
        )
        .then(response => response.json)
        .then(data => {

            Object.keys(data).filter((element) => {

                if (data[element].studentName.toLowerCase() === id.toLowerCase()) {
                    studentData = {
                        studentName: data[element].studentName,
                        yearGroup: data[element].yearGroup,
                        currentSchool: data[element].currentSchool,
                        parentOrGuardian: data[element].parentOrGuardian,
                        phoneNumber: data[element].phoneNumber,
                        homeAddress: data[element].homeAddress
                    };
                }

                return studentData;

            }).map((filteredElement) => data[filteredElement])
            
        })

    }
    
    
    return (

        <div className="btn-textfield">

            <button className="btn-textfield" onClick={infoGetter}>
                Id: {id}
                <br/><br/>
                StudentInfo:
                <br/><br/>
                {studentData.studentName}
                {studentData.yearGroup}
                {studentData.currentSchool}
                {studentData.parentOrGuardian}
                {studentData.phoneNumber}
                {studentData.homeAddress}
            </button>

        </div>

    );

}

export default StudentInfo;

I think the problem is assigning the value to the object or sorting through. I appreciate any responses.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I am not actually sure why you filtering your data object without taking your student ID in your consideration, but what I am going to do is to

Object.keys(data).filter(element => element.id === id ).map(student => {
  // You will be able to find your current **student** right her
  console.log(student);
})

There is no need to use the Data object again in your if condition

about 4 years ago · Juan Pablo Isaza Relatório

0

Sorry. It was a dumb mistake. I did response => response.json forgetting the parenthsis.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda