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
- [Vuejs]-Is delete a function or reserved keyword in Vue?
- [Vuejs]-Created hook not waiting for beforeEach action promise to resolve
Source:stackexchange.com