Files
school-developer/src/utils/dictLabel.ts
guochunsi 8166fa31e0 a
2026-01-14 18:32:09 +08:00

36 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 通用:根据字典列表和 value 获取 label 文本
export function getLabelValue(
list: Array<{ label: string; value: string | number }>,
value: string | number | null | undefined,
): string {
if (!list || !Array.isArray(list)) return ''
const item = list.find((it: any) => String(it.value) === String(value))
return item ? item.label : ''
}
// 通用:根据指定 key/value 字段,从列表中取 label原 global.getLabelValueByPropes
export function getLabelValueByProps(
list: any[],
key: string | number | null | undefined,
props: { key: string; value: string },
): string {
if (!list || !Array.isArray(list)) return ''
const item = list.find((it: any) => String(it[props.key]) === String(key))
return item ? item[props.value] : ''
}
// 通用:根据 key 取“专业名称 || 学制年制”展示(原 global.getLabelValueByPropes2
export function getMajorLabelWithYears(
list: any[],
key: string | number | null | undefined,
props: { key: string; value: string },
): string {
if (!list || !Array.isArray(list)) return ''
const item = list.find((it: any) => String(it[props.key]) === String(key))
if (!item) return ''
const majorName = item[props.value] ?? ''
const years = item.xz != null ? `${item.xz} 年制` : ''
return years ? `${majorName} || ${years}` : majorName
}