Tengo una matriz que contiene algunos de estos 3 valores: ['daily', 'monthly', 'yearly']
pero a veces la matriz solo contiene: ['monthly', 'daily']
y lo que quiero es obtener el mínimo entre estos 3 valores que es daily y si no hay quiero monthly y también si no hay quiero el yearly . ¿Cómo puedo lograr eso?
Puede abordar este problema comparando los valores daily , monthly y yearly alfabéticamente/por orden ASCII
El enfoque más simple que no se basa en el orden alfabético (y, por lo tanto, aún funcionaría al agregar nuevos intervalos como hourly ):
function getLowest (arr) { const order = ['daily', 'monthly', 'yearly'] return order.find(val => arr.includes(val)) } Esto funciona porque find devolverá el primer resultado coincidente.
Aquí está la función que solo necesita para pasar su matriz, devolverá un valor mínimo.
function getMinimum(arr) { return arr.includes('daily') ? 'daily' : arr.includes('monthly') ? 'monthly' : arr.includes('yearly') ? 'yearly' : ""; } var arr1 = ['daily', 'monthly', 'yearly']; console.log(getMinimum(arr1));