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

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

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 Report

0

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

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!