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

150
Visualizações
Remove shorter items in array that are already present

I'm trying to remove items from array that are present already in longer items. For Example if I have two items 'Knowledge base' and 'base' I want to remove 'base', if I have knowledge management multiple times I want to leave all items unless there is an item that contain those two words but it's longer for example 'knowledge management book'.

It's easy to sort items by length but I'm not sure how to check for example if 'base' is already present in longer term('Knowledge base')

So in the example below desired solution would be:

const items = ['Knowledge base', 'knowledge management', 'knowledge management']

const items = ['Knowledge base', 'base', 'management', 'knowledge management', 'Knowledge', 'knowledge', 'knowledge management']

const newItems = items.sort((a,b) => b.length - a.length)

console.log(newItems)

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

0

You can use Array.filter() along with Array.some() to filter any items that are included in other items.

const items = ['Knowledge base', 'base', 'management', 'knowledge management', 'Knowledge', 'knowledge', 'knowledge management'];

const result = items.filter(item => !items.some(it => it !== item && it.includes(item)));

console.log('Result:', result);

about 4 years ago · Juan Pablo Isaza Relatório

0

Filter by some and verify by includes methods

const items = ['Knowledge base', 'base', 'management', 'knowledge management', 'Knowledge', 'knowledge', 'knowledge management'];

const newItems = items.filter((item) => {
      return !items.some((iChecker) => {
           return iChecker !== item && iChecker.includes(item)
         }
      ) 
  }
);

console.log( { newItems } );

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