123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- import XEUtils from "xe-utils";
- interface Option {
- // 数组中的 code
- listCode: string,
- // 数组中的 name
- listValue: string
- }
- const defaultValue: Option = {
- listCode: 'code',
- listValue: 'name'
- }
- /**
- *
- * @param arr 数组
- * @param code 需要寻找的值
- * @param option 配置
- * @return str {string} name
- */
- const findValueByListCode = (arr: any[] | object, code: string | number, option: Option = defaultValue): string => {
- const {listCode, listValue} = option
- if (XEUtils.isEmpty(arr)) {
- return '';
- }
- if (!XEUtils.isArray(arr)) {
- return '';
- }
- if (typeof code === 'undefined' || code === null) {
- return '';
- }
- for (let i: number = 0, len: number = arr.length; i < len; i++) {
- if (arr[i][listCode] === code) {
- return arr[i][listValue];
- }
- }
- return ''
- }
- export default findValueByListCode;
|