vue.jsvue-filter

Changing displayed value depending on a second digit from a number


I have a vue filter and display some values. I would like to change the value when the value's last digit is between 2 and 4. I need to have an automatised code so it works for all possible intervals, for example: (22-24, 32-34, 622-624...). I managed to find the last digit of a value: value.toString()[value.toString().length - 1]. I am not sure how to solve this problem, maybe I should come up with a special matematical formula?

myfilter: (value): => {
    if(value >= 22 && value <= 24) {
        return 'my new value'
    }
}

Solution

  • You can use the modulus operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Remainder_()

    myfilter: (value): => {
        if(value % 10 <= 4 && value % 10 >= 2 ) {
            return 'my new value'
        }
    }