记录第一次bug(末尾有逗号)

59 阅读1分钟

image.png

错误原因

由于每次都在末尾加了逗号,导致最后形成的字符串末尾有逗号。

改进方法

        getString(item) {
            let parts = [];

            const { authScope: { person = [], roles = [], orgScopesWithSub = [], orgScopesWithoutSub = [] } } = item

            if (person.length > 0) {
                parts.push('指定人员:' + person.map(item => item.name).join('、'));
            }

            if (orgScopesWithoutSub.length > 0) {
                parts.push('指定部门(不含下级部门):' + orgScopesWithoutSub.map(item => item.name).join('、'));
            }

            if (orgScopesWithSub.length > 0) {
                parts.push('指定部门(含下级部门):' + orgScopesWithSub.map(item => item.name).join('、'));
            }

            if (roles.length > 0) {
                parts.push('指定角色:' + roles.map(item => item.name).join('、'));
            }

            return parts.length === 0 ? '--' : parts.join(',');
        },
将parts定义为数组,再通过join方法改为字符串(同时保证了中间有逗号,末尾没有逗号)