Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass argument to callback arrow function (JavaScript array.every)

I have a callback function which can be re-usable based on the argument that is passed while calling the function, It will be helpful to decide the behavior of callback function

Callback function:

  sampleArray = [5, 3, 4, 6, 7];

  getProcessedData = (value, args) => {
    console.log('manoj', args);
    if(args === 'value a') {
      ...
    } else if (args === 'value b') {
      ...
    }
    return;
  };

I using array.every() function on the above array

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

sampleArray.every(getProcessedData, 'value A');
sampleArray.every(getProcessedData, 'value B');

But I’m not able to receive this ‘value A’ in getProcessedData() function, I have tried with few other way, but it didn`t work. Any suggestion would be helpful

>Solution :

Use an anonymous function that passes the second argument.

sampleArray.every(el => getProcessedData(el, 'value A'));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading