Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda