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

109
Visualizações
I need a good way to classify files names in each categories from an array in javascript, but when not having a match put into a general group

Basically, I have two arrays, one is the types of the files and the other is the actually files names, so, I need to verify each of the file name's and search for an specific string, for then, group up, but has some rules, like, the group has to be in order name ASC and the general has to have either the ones that doesn't match with an type and the ones with the type in it, for the new versions files. (I need to Do this with pure Javascript) Examples:

types: ["general","picture","document","annotation"]
files: [
         {name:"-t_picture_t-my_vacation.jpg"},
         {name:"-t_document_t-my_curriculum.pdf"},
         {name:"my_favorite_music.mp3"},
         {name:"my_dance_video.mp4"},
         {name:"-t_annotation_t-dont_forget.txt"},
       ]

The results expected are something like:

[
 {name:"my_dance_video.mp4"},
 {name:"my_favorite_music.mp3"},
 {name:"-t_picture_t-my_vacation.jpg"},
 {name:"-t_document_t-my_curriculum.pdf"},
 {name:"-t_annotation_t-dont_forget.txt"},
]

Any questions about, just send a message.

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

0

Well... a simple algorithm will be something like this

const types = ["picture","document","annotation", "general"]
const files = [
         {name:"-t_picture_t-my_vacation.jpg"},
         {name:"-t_document_t-my_curriculum.pdf"},
         {name:"my_favorite_music.mp3"},
         {name:"my_dance_video.mp4"},
         {name:"-t_annotation_t-dont_forget.txt"},
       ]
       
const result = []
const matches = {}

for (const file of files) {
    for (const type of types) {
        if (file.name.includes(type)) {
          result.push(file)
          matches[file.name] = true
        } else if (type == 'general' && !matches[file.name]) {
            result.unshift(file)
        }
    }
}

console.log(result)

The downside is that we need to wait with general category till the end because only when we complete sorting we can add the rest of items without duplication

Hope that helps for you to kick-start your solution. Enjoy 😎

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