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

560
Visualizações
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 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

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 4 years ago · Santiago Gelvez Relatório

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 4 years ago · Santiago Gelvez 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