如果将一个类规则数据转成一个文字型描述?

我有这样一段数据:

{
        type: 'root',
        key: '8120abb2-d5c8-4843-8170-0ac1fc07a6c1',
        conditionValue: 'and',
        conditionLabel: '并且',
        children: [
          {
            type: 'group',
            label: '规则组1',
            key: '143c2a99-8316-45aa-8bdf-6c5aea09ca56',
            conditionValue: 'or',
            conditionLabel: '或者',
            children: [
              {
                type: 'dimension',
                label: '维度1',
                key: 'fa155535-f339-44d6-b368-32ac7f10b83f',
                conditionValue: 'or',
                conditionLabel: '或者',
                children: [
                  {
                    type: 'rule',
                    label: '规则1',
                    key: '08979aee-221f-49a4-991c-39306317ecdc',
                    dimensionLabel: '职位',
                    dimensionValue: '1',
                    conditionLabel: '等于',
                    conditionValue: '=',
                    inputLable: '1',
                    inputValue: '1',
                  },
                  {
                    type: 'rule',
                    label: '规则-2',
                    key: '78aa74dc-4982-4a9f-93c4-5a16fdf0ea76',
                    dimensionLabel: '职位',
                    dimensionValue: '1',
                    conditionLabel: '等于',
                    conditionValue: '=',
                    inputLable: '2',
                    inputValue: '2',
                  },
                  {
                    type: 'rule',
                    label: '规则-3',
                    key: '8c760cf5-e042-4b33-ab08-c42282d74ed6',
                    dimensionLabel: '职位',
                    dimensionValue: '1',
                    conditionLabel: '等于',
                    conditionValue: '=',
                    inputLable: '3',
                    inputValue: '3',
                  },
                ],
              },
              {
                type: 'dimension',
                label: '维度2',
                key: '30357f52-2773-4dd2-9775-88f55ba5fa77',
                conditionValue: '',
                conditionLabel: '',
                children: [
                  {
                    type: 'rule',
                    label: '规则1',
                    key: 'db6e0e61-2374-43c9-8a3e-176cf6e9221b',
                    dimensionLabel: '职级',
                    dimensionValue: '2',
                    conditionLabel: '不等于',
                    conditionValue: '!==',
                    inputLable: 'a',
                    inputValue: 'a',
                  },
                ],
              },
              {
                type: 'dimension',
                label: '维度3',
                key: '25864967-8226-494d-b1a5-536d5ad78eb1',
                conditionValue: '',
                conditionLabel: '',
                children: [
                  {
                    type: 'rule',
                    label: '规则2',
                    key: '4ba45def-5b59-4022-8f41-c382ef1e98aa',
                    dimensionLabel: '职级',
                    dimensionValue: '2',
                    conditionLabel: '不等于',
                    conditionValue: '!==',
                    inputLable: 'b',
                    inputValue: 'b',
                  },
                ],
              },
            ],
          },
          {
            type: 'group',
            label: '规则组2',
            key: 'c1a86886-5f07-4454-915d-48370218aaaa',
            conditionValue: '',
            conditionLabel: '',
            children: [
              {
                type: 'dimension',
                label: '维度1',
                key: '1785ce8f-55b4-4d5a-97b2-4b80244ebb17',
                conditionValue: '',
                conditionLabel: '',
                children: [
                  {
                    type: 'rule',
                    label: '规则1',
                    key: 'e5b9f804-46f0-4082-ac40-024a5907248f',
                    dimensionLabel: '职位',
                    dimensionValue: '1',
                    conditionLabel: '等于',
                    conditionValue: '=',
                    inputLable: 'cc',
                    inputValue: 'cc',
                  },
                ],
              },
            ],
          },
        ],
      }

我想编写一个函数,该函数最终的返回结果为:
((职位 = 1 or 职位 = 2 or 职位 = 3) or (职级 != a or 职级 != b)) and (职位 = cc)

目前我用递归没写出来

阅读 555
2 个回答
// (((职位 = 1 or 职位 = 2 or 职位 = 3) or (职级 !== a) or (职级 !== b)) and ((职位 = cc)))
function rule2text(rule) {
  if(rule.type === 'rule') return [rule.dimensionLabel, rule.conditionValue ,rule.inputValue].join(' ');
  return '('+rule.children.map(rule2text).join(' ' + rule.conditionValue + ' ')+')'

除了括号有多余的好像没啥问题

你这个数据是不是有问题,(职级 != a or 职级 != b)这段的or是从哪里推出来的

按我理解应该是((职位 = 1 or 职位 = 2 or 职位 = 3) or (职级 != a) or (职级 != b)) and (职位 = cc)吧?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题