Search code examples
javascriptlodash

how to specify two values for the second argument for lodash orderBy?


Could anyone help me how to realize the following case: I have an array with objects, every object has field createEL, and some of them have updateEl.

My task is to sort all the elements with updateEl if it's available, otherwise sort by createEl

my current option:

orderBy(myData, ['updateEl','createEl'], 'desc)

I wonder if it's correct for cases if I don't have updateEl option.

Thanks in advance for every support


Solution

  • Use a function that returns either updateEl or createEl as the field to order by.

    orderBy(myData, el => el.updateEl || el.createEl)