Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!