@wufengteam/inputs
Version:
平台提供的右侧属性编辑器,需要在主工程中注册
44 lines • 2.15 kB
JavaScript
/**
* 获取出参中数据类型为数组的所有属性
* @param data json对象数组
* @returns
*/
export function traverseAndExtractCodes(data) {
var result = [];
function extractCodes(item, path) {
var _a, _b;
if ((item === null || item === void 0 ? void 0 : item.attrType) && ((_a = item === null || item === void 0 ? void 0 : item.attrType) === null || _a === void 0 ? void 0 : _a.toLowerCase().includes('array'))) {
// 如果当前项attrType包含'array',则添加其code到路径中
// path.push(item.code);
// 将当前路径作为value添加到结果中
var value = '';
if (path) {
value = "".concat(path, ".").concat(item === null || item === void 0 ? void 0 : item.code);
} else {
value = "".concat(item === null || item === void 0 ? void 0 : item.code);
}
result.push(Object.assign(Object.assign({}, item), {
value: value,
label: "".concat(item.name || (item === null || item === void 0 ? void 0 : item.code), "\uFF08\u6570\u7EC4\uFF09")
})); // 使用slice()来创建路径的副本,避免后续修改影响结果
}
if ((item === null || item === void 0 ? void 0 : item.children) && Array.isArray(item === null || item === void 0 ? void 0 : item.children)) {
// 遍历children数组
(_b = item === null || item === void 0 ? void 0 : item.children) === null || _b === void 0 ? void 0 : _b.forEach(function (child) {
// 递归调用extractCodes,并将当前路径传递给下一层
extractCodes(child, "".concat(path, ".").concat(item === null || item === void 0 ? void 0 : item.code)); // 将当前项的code添加到路径中
});
}
}
// 对数组中的每个项调用extractCodes函数,初始路径为空数组
data === null || data === void 0 ? void 0 : data.forEach(function (item) {
return extractCodes(item, '');
});
// 替换将第一位的'.'为空字符串
var resultArray = result.map(function (item) {
return Object.assign(Object.assign({}, item), {
value: item.value.replace(/^(\.|\.\.|\.\.\.)/, '.')
});
});
return resultArray;
}