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

161
Views
Search for data using filter function not working

I am having problems with rendering the filter function. I think the problem is that I am not returning smth , but I am not so sure. I am using react and a json file to display data. this is the warning that I get on my console "react-jsx-dev-runtime.development.js:117 Warning: Each child in a list should have a unique "key" prop.

Check the render method of SearchTable. See https://reactjs.org/link/warning-keys for more information. at tr"

The code:

import data from "../data.json";

const SearchTable = () => {
    const [searchTerm, setSearchTerm] = useState("");

  return (
    <div className="container">
      <input 
      type="text"
       placeholder="Search..." 
       className="form-control" 
       style ={{marginTop:50 ,marginBottom: 20, width : "40%"}} 
       onChange= {(e)=>{
        setSearchTerm(e.target.value); 
      }}
       />

      <table className="table table-bordered">
        <thead className="thead-dark">
          <tr>
            <th>Number</th>
            <th>PartitionId</th>
            <th>enqueueTime</th>
          </tr>
        </thead>
        <tbody>
          {data.filter((val) => {
              if(searchTerm === ""){
                  return val;
              } 

              else if (
                  val.partitionId === searchTerm ||
                  val.enqueueTime === searchTerm
                
              ) {
                console.log("hej")
                  return val;
                  
              }
          }).map((m) => (
            <tr key={m.id}>
              <td>{m.Nr}</td>
              <td>{m.partitionId}</td>
              <td>{m.enqueueTime}</td>
            </tr>
          ))}
        </tbody>
      </table>
    </div>
  );
};

export default SearchTable;```
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If I were you, I would remove business logic from the template. What is more, the function that is passed to the filter method should return true if an entry should be placed in a filtered array or false otherwise. I put below the code with a generic filter function. One more thing, if the string is empty, there is no reason to call a filter on the array. If you don't want to filter, don't use CPU if you don't need to ;)

PS. you used non-existent object key when rendering the items. Change it either to Nr or partitionId.

PPS. We should change searched string to lower case as should entries within the data object.

Cheers! If you have any questions, I'm happy to answer them.

import data from "../data.json";
import React, { useState, useEffect } from 'react';

const SearchTable = () => {
    const [searchTerm, setSearchTerm] = useState("");
    const [filteredData, setFilteredData] = useState(data);
  
    const filterData = () => {
      if (searchTerm === '') {
        setFilteredData(data);
        return;
      }
      
      setFilteredData(data.filter(entry => {
        return Object.keys(entry).some(entryKey => {
          const entryValue = entry[entryKey].toString().toLocaleLowerCase();
          
          return entryValue.contains(searchTerm);
        })
      }));
    };
  
    useEffect(filterData, [searchTerm]);

  return (
    <div className="container">
      <input 
      type="text"
       placeholder="Search..." 
       className="form-control" 
       style ={{marginTop:50 ,marginBottom: 20, width : "40%"}} 
       onChange= {(e)=>{
        setSearchTerm(e.target.value.toLocaleLowerCase()); 
      }}
       />

      <table className="table table-bordered">
        <thead className="thead-dark">
          <tr>
            <th>Number</th>
            <th>PartitionId</th>
            <th>enqueueTime</th>
          </tr>
        </thead>
        <tbody>
          {filteredData.map((m) => (
            <tr key={m.Nr}>
              <td>{m.Nr}</td>
              <td>{m.partitionId}</td>
              <td>{m.enqueueTime}</td>
            </tr>
          ))}
        </tbody>
      </table>
    </div>
  );
};

export default SearchTable;
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!