You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.0 KiB
44 lines
1.0 KiB
|
3 years ago
|
import each from './each';
|
||
|
|
import getType from './getType';
|
||
|
|
import trim from './trim';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @description: 把不规则的数据格式转换为统一的数组[{value:'1',label:'文字'}]数据格式
|
||
|
|
* @param {Array|object} data
|
||
|
|
* @param {string} valueKey
|
||
|
|
* @param {string} labelKey
|
||
|
|
* @return {Array}
|
||
|
|
*/
|
||
|
|
export default function toValidListData(data, valueKey = 'value', labelKey = 'label') {
|
||
|
|
const list = [];
|
||
|
|
const type = getType(data);
|
||
|
|
if (type === 'object') {
|
||
|
|
each(data, (item, k) => {
|
||
|
|
const obj = {};
|
||
|
|
if (trim(valueKey)) {
|
||
|
|
obj[valueKey] = k;
|
||
|
|
}
|
||
|
|
if (trim(labelKey)) {
|
||
|
|
obj[labelKey] = item;
|
||
|
|
}
|
||
|
|
list.push(obj);
|
||
|
|
});
|
||
|
|
} else if (type === 'array') {
|
||
|
|
each(data, item => {
|
||
|
|
if (getType(item) === 'object') {
|
||
|
|
list.push(item);
|
||
|
|
} else {
|
||
|
|
const obj = {};
|
||
|
|
if (trim(valueKey)) {
|
||
|
|
obj[valueKey] = item;
|
||
|
|
}
|
||
|
|
if (trim(labelKey)) {
|
||
|
|
obj[labelKey] = item;
|
||
|
|
}
|
||
|
|
list.push(obj);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
return list;
|
||
|
|
}
|