Skip to content
Advertisement

passing props directly to utility function

i have below common function for sorting,

JavaScript

and i am calling above function like as below,

JavaScript

for numeric type i am sorting based on nominalSize, now i would like to pass this field directly from here sortSelectOptions(options, null, 'numeric') as prop. The options are array of objects and one of the property is nominalSize. I am looking kind of generic sorting function.

Could any one please let me know how can i pass exact field as a prop to common function to achieve sorting functionality.

many thanks in advance

Advertisement

Answer

If I understand you correctly, you want the logic for accessing the value to sort by outside of the sort function?

You can make properties accessible through their name like so:

JavaScript

And call it with:

JavaScript

Or you can generalize this pattern and ask for an accessor function like so:

JavaScript

And call it with:

JavaScript

The latter version would allow you to also access deeply nested properties.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement