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

169
Views
How to change a function based on unknown number of arguments in javascript

Is there any way to add to or manipulate expressions in a returned function?

This is an example for a single argument:

function trackByProp(prop) {
  return function (value) {
    return value[prop];
  };
}

The aim is to extend this function to allow multiple props to be added e.g.

function trackByProp(...props) {
  props.forEach((prop) => {
    //Somehow add value[prop] to the return function???
  });

  return function (value) {
    return; // value[arg1] + value[arg2] + value[argN]
  };
}

Alternatively is there a simpler way to create this function ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

function trackByProps(...props) {
  return function (value) {
    return props.reduce((result, prop) => result + value[prop], "")
  }
}

Using the properties in the inner function ensures they are available in a closure. similar to the single argument example. Then they can be iterated over each time the inner function is called.

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