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

182
Vistas
How to get the years from an array of dates in order

I have this array:

var array1 = ['9/2022','7/2020','11/2021','12/2020','2/2020','4/2021','10/2021'];

How can I get the years from it in order? So the result would be this:

result = ['2020','2021','2022'];

Do I have to change the array to a new Date() format and sort, or how would be the best way ? Any Suggestion ?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Using a combination of ... , Set , .map() and .sort(). This can be done in one line.

... -> destructures array into individual items.

new Set() -> make a set out of the new items.

map() -> runs a loop and map the array into a new one.

split() -> breaks a string into array.

sort() -> sorts an array.

var array1 = ['9/2022','7/2020','11/2021','12/2020','2/2020','4/2021','10/2021'];

console.log([...new Set(array1.map(x => x.split('/')[1]))].sort());

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can easily achieve this result using Set and map

var array1 = [
  "9/2022",
  "7/2020",
  "11/2021",
  "12/2020",
  "2/2020",
  "4/2021",
  "10/2021",
];

const result = [...new Set(array1.map((s) => s.split("/")[1]))].sort();

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can map to get only the years, and then sort the results. Then you instantiate a Set which removes all duplicate entries.

new Set(['9/2022','7/2020','11/2021','12/2020','2/2020','4/2021','10/2021']
  .map(dateAsString => dateAsString.split('/')[1])
  .sort());
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