• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

475
Views
How to write function that filters a dictionary when given a key/value pair in Javascript?

I have a dictionary called teamData

var teamData = {
     app: {
      sortCol:"name",
      sortDir:"asc"
     },
     data: [
       {
         id: 1,
         name:"Raptors",
         coachId: 1,
         coachFirst: "Ken",
         coachLast: "jenson",
         coachPhone: "801-333-4444",
         coachEmail: "ken.jenson@uvu.edu",
         coachLicenseLevel: 1,
         league: 1,
         division: 1
       },
        {
         id: 2,
         name:"Killer Bunnies",
         coachId: 2,
         coachFirst: "Peter",
         coachLast: "Rabbit",
         coachPhone: "801-333-4444",
         coachEmail: "peter.rabbit@uvu.edu",
         coachLicenseLevel: 1,
         league: 1,
         division: 2
       },
       {
         id: 3,
         name:"Thunderbirds",
         coachId: 3,
         coachFirst: "Harry",
         coachLast: "DirtyDog",
         coachPhone: "801-333-4444",
         coachEmail: "harry.dirty.dog@uvu.edu",
         coachLicenseLevel: 2,
         league: 1,
         division: 2
       }
     ]
     
   }

I'm trying to write a function that takes a key/value object and returns a filtered dictionary. So if the function is

let teams = filter({coachLicenseLevel:1});

then the expected result is to return a filtered dictionary with only two elements that have that key/value pair

Here is the function I have so far, but I'm stuck on how to get the key object.

filter(filterObj) {
        const v = Object.values(filterObj);
        const k = Object.keys(filterObj);
        
        const res = teamData.filter(({???}) => v.includes(???));
    }

any help would be appreciated.

about 3 years ago · Santiago Gelvez
2 answers
Answer question

0

If you want to filter only the data array, you could do something like this:

 function filterArrayByParamAndValue(arr, itemParam, value) {
  return arr.filter(item => item.itemParam === value)

}

And in your code just replace the data property, if

let teamData = {
....,
data: [...filterArrayByParamAndValue(teamData.data, coachLicenseLevel, 1)],
....
}

Of course you should also add all necessary checks in the filter function, or even add an object property to check for and pass the whole object.

about 3 years ago · Santiago Gelvez Report

0

Instead of passing an object, you may consider using the filter function with your custom filter logic. Here is an example for your specific case:

let teams = teamData.data.filter(item => item.coachLicenseLevel == 1)
about 3 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error