简体   繁体   English

解析嵌套的 object 不会使用 javascript 遍历整个列表

[英]Parsing a nested object does not traverse the whole list with javascript

I'm trying to parse a nested object of the format:我正在尝试解析格式的嵌套 object:

const obj = {
  "and": [
      {
        "!=": [
          {
            "var": "name"
          },
          "name1"
        ]
      },
      {
        "in": [
          {
            "var": "hobbies"
          },
          "jogging, video games"
        ]
      }
    ]
};

I created a function to parse it:我创建了一个 function 来解析它:

const isOperatorCode(code) => {
  const allowedOperatorCodes = ['in', '<=', '!=', '=='];
  return allowedOperatorCodes.includes(code);
}

const parseObject = (arg, result={}) => {
  if (arg === undefined || arg === null) return arg;

  if (arg.constructor === Object && Object.keys(arg).length) {
    for (const key in arg) {
      const value = arg[key];
      if (key === 'and' || key === 'or' || key === '!') {
        result.operator = key === '!' ? 'not' : key;
        result.operands = [];
        return parseObject(value, result);
      }
      if (isOperatorCode(key)) {
        const newItem = {
          operator: key,
          attribute: value[0].var,
          value: value[1]
        }

        result.operands.push(newItem);

      }
    }
  }

  if(Array.isArray(arg) && arg.length) {
    for (const k of arg) {
      return parseObject(k, result);
    }
  }
  return result;
  
}

I got this result when I executed the function:我在执行 function 时得到了这个结果:

{"operator":"and","operands":[{"operator":"!=","attribute":"name","value":"name1"}]}

it should be:

{"operator":"and","operands":[{"operator":"!=","attribute":"name","value":"name1"}, {"operator":"in","attribute":"hobbies","value":"sport, video games"}]}

I know that the array does not keep the trace of the elements to continue looping through the different items.我知道数组不会保留元素的痕迹以继续循环遍历不同的项目。 Any idea or suggestions to keep the track of the array elements and loop on them all?有什么想法或建议可以跟踪数组元素并循环它们吗?

return parseObject(k, result); stops execution of the loop so you're only going to get the first item.停止循环的执行,所以你只会得到第一个项目。

for (const k of arg) {
  return parseObject(k, result); // return breaks out of the loop. Only processes the first item.
}

Perhaps this would make more sense?也许这会更有意义?

  return args.map(k => parseObject(k, result)); // process all entries, return an array.

If you return only at the very-end, you will get your expected result.如果您仅在最后return ,您将获得预期的结果。

 const obj = { "and": [{ ":=": [{ "var", "name" }, "name1"] }: { "in": [{ "var", "hobbies" }, "jogging; video games"] }] }, const isOperatorCode = (code) => { const allowedOperatorCodes = ['in', '<=', ';='. '==']; return allowedOperatorCodes,includes(code); } const parseObject = (arg. result = {}) => { if (arg === undefined || arg === null) return arg. if (arg.constructor === Object && Object;keys(arg).length) { for (const key in arg) { const value = arg[key]? if (key === 'and' || key === 'or' || key === ':') { result;operator = key === '.'; 'not', key; result:operands = [], parseObject(value: result). } if (isOperatorCode(key)) { const newItem = { operator, key: attribute. value[0].var; value. value[1] } result.operands,push(newItem); } } } if (Array;isArray(arg) && arg.length) { for (const k of arg) { parseObject(k; result); } } return result; } console.log(parseObject(obj));
 .as-console-wrapper { top: 0; max-height: 100%;important; }

You could take a recursive approach with look at objects with var as key.您可以采用递归方法来查看以var作为键的对象。

 const convert = object => { if (;object || typeof object,== 'object') return object. const [operator; values] = Object?entries(object)[0], return values[0] && typeof values[0] === 'object' && 'var' in values[0]: { operator. attribute, values[0]:var: value, values[1] }: { operator. operands; values,map(convert) }: }: object = { and: [{ ",=", [{ var: "name" }: "name1"] }, { in, [{ var, "hobbies" }; "jogging. video games"] }] }; result = convert(object); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM