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

119
Visualizações
How do I filter an object with an object using Array.filter similar to lodash?

I am trying to avoid importing lodash for a simple filter. I am trying to do something similar to this...

// Items
[
  { "title": "a", "name": "a", "location": "here"},
  { "title": "b", "name": "a", "location": "here"}, 
  { "title": "d", "location": "there"}, 
]
// Filter
{ "name" : "a", "location": "here" }

Should return the first two items. In lodash this is done by something similar to _.filter(Items, Filter). I would like to implement this without lodash or a bunch of code and getting Object.keys. Is there a simple way in modern Javascript to handle this?

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

0

Here's what I came up with, seems to work well

let foo = [
  { "title": "a", "name": "a", "location": "here"},
  { "title": "b", "name": "a", "location": "here"}, 
  { "title": "d", "location": "there"}, 
];

let bar = { "name" : "a", "location": "here" };

let result = foo.filter(obj =>
  Object.entries(bar).every(([k,v]) => obj[k]===v)
);

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

I don't believe there's a "simple way in modern Javascript to handle this" but if you don't have a hard requirement on avoiding Object.keys you could write a helper function to do this:

const myItems = [
  { "title": "a", "name": "a", "location": "here"},
  { "title": "b", "name": "a", "location": "here"}, 
  { "title": "d", "location": "there"}, 
]

const myFilter = { "name" : "a", "location": "here" };

const filter = (items, filterBy) => 
  items.filter(item => 
    Object.keys(filterBy).every(key => item[key] === filterBy[key])
  )

alert(JSON.stringify(filter(myItems, myFilter)));

about 4 years ago · Juan Pablo Isaza Relatório

0

You could get the entries of the filter in advance to prevent getting th entries for every filtering iteration.

const
    filter = (array, filter) => {
        const
            filterBy = entries => o => entries.every(([k, v]) => o[k] === v);

        return items.filter(filterBy(Object.entries(filters)));
    },
    items = [{ title: "a", name: "a", location: "here" }, { title: "b", name: "a", location: "here" }, { title: "d", location: "there"}],
    filters = { name: "a", location: "here" },
    result = filter(items, filters);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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