let mapList = {
1: "一级告警",
2: "二级告警",
3: "三级告警",
};
const findKeyByValue = (obj, value) => {
for (let key in obj) {
if (obj[key] === value) {
return key;
}
}
// 如果未找到匹配的值,可以返回一个特殊值或抛出异常,具体根据需求而定
return null;
};
const value = '二级告警';
const key = findKeyByValue(mapList, value);
console.log(key); // 输出 2
最常用的下拉框使用这个
const list = [
{ label: 'yyy', value: '1' },
{ label: 'uuu', value: '2' },
{ label: 'ppp', value: '3' }
];
const key = 'yyy';
function getLabel(obj, keys) {
let result;
obj.forEach(e => {
if (e.label === keys) {
result = e.value;
}
});
return result;
}
console.log('getLabel', getLabel(list, key));