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

132
Vistas
javascript function argument use as variable

I'm practicing my shorthand code writing.

Originally the code goes like

function sort(sortBy: String) {
    SortByAscending = !SortByAscending
    rankingList.sort((a: any, b: any) => {
        if(sortBy == 'name')
        return a.name < b.name ? SortByAscending ? -1 : 1 : SortByAscending ? 1 : -1
        if(sortBy == 'age')
        return a.age < b.age ? SortByAscending ? -1 : 1 : SortByAscending ? 1 : -1
    }
    );
}

instead of writing "if(sortBy == etc)", is there a way to use template literal like

function sort(sortby: String) {
    SortByAscending = !SortByAscending
    rankingList.sort((a: any, b: any) => {
        return a.`${sortby}` < b.`${sortby}` ? SortByAscending ? -1 : 1 : SortByAscending ? 1 : -1
    }
    );
}

using the argument as the string?

about 4 years ago · Juan Pablo Isaza
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