[Vuejs]-How could I loop through an array and return the values ​dynamically?

6đź‘Ť

Use Array.find to find the matching value dynamically.

const AvailableUserRoles = [
    {
        label: 'Administrator',
        value: 1
    },
    {
        label: 'Count',
        value: 2
    },
    {
        label: 'Test',
        value: 5
    }
]
function getValue(item) {
    const matchItem = AvailableUserRoles.find(node => node.value === item);
    return matchItem ? matchItem.label : "";
}
console.log(getValue(1)); // Returns 'Administrator'
console.log(getValue(2)); // Returns 'Count'
console.log(getValue(5)); // Returns 'Test'
console.log(getValue(6)); // Returns ''
👤Nitheesh

Leave a comment