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

223
Visualizações
How to filter out items in array of objects based on item in property array in JavaScript

What is the best way to return items in an array of object if they contain a certain property in a property which is an array?

I have the following array of objects:

const menus = [
  {
    id: 1,
    title: 'English',
    locations: ['MENU', 'MENU_EN']
  },
  {
    id: 2,
    title: 'Spanish',
    locations: ['MENU', 'MENU_SP']
  },
  {
    id: 3,
    title: 'German',
    locations: ['MENU', 'MENU_DE']
  },  
  {
    id: 4,
    title: 'German and english',
    locations: ['MENU', 'MENU_EN', 'MENU_DE']
  },
]

As an example, I am looking for a way to return any items which have 'MENU_EN' in the locations property. So I need a function which will return the following:

const filteredMenus = [
  {
    id: 1,
    title: 'English',
    locations: ['MENU', 'MENU_EN']
  },  
  {
    id: 4,
    title: 'German and english',
    locations: ['MENU', 'MENU_EN', 'MENU_DE']
  },
]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can filter menus array using Array.prototype.filter() by checking if locations property includes the desired MENU_EN with Array.prototype.includes():

Code:

const menus = [{id: 1,title: 'English',locations: ['MENU', 'MENU_EN']},{id: 2,title: 'Spanish',locations: ['MENU', 'MENU_SP']},{id: 3,title: 'German',locations: ['MENU', 'MENU_DE']},{id: 4,title: 'German and english',locations: ['MENU', 'MENU_EN', 'MENU_DE']},]

const filteredMenus = menus.filter(({ locations }) => locations.includes('MENU_EN'))

console.log(filteredMenus)

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