更新for循环中的嵌套对象

时间:2020-06-04 05:45:43

标签: javascript

我遇到以下问题,我有一组这样的JSON规则

{
    "event": {
        "type": "maxrulecount",
        "params": {
            "maxrulecount": 2
        }
    },
    "conditions": {
        "any": [
            {
                "all": [
                    {
                        "fact": "apples",
                        "value": "20",
                        "operator": "greaterThanInclusive"
                    }
                ]
            },
            {
                "all": [
                    {
                        "fact": "bananas",
                        "value": "100",
                        "operator": "greaterThanInclusive"
                    }
                ]
            }
        ]
    }
}


所以我显然将其转换为对象,但是数字值仍然是字符串,因此我创建了一个函数,该函数会将字符串中的任何数字转换为类似这样的数字

checkForNumberValues(rules) {
    // allows any number of numbers together or a decimal number
    let numberRegex = /^(([0-9]{1,})|([0-9]{1,}\.[0-9]{1,}))$/g;
    // yes a loop within a loop but time complexity is no issue here
    rules?.conditions?.any?.forEach((condition) => {
      condition?.all?.forEach((rule) => {
        console.log(rule.value, numberRegex.test(rule.value)); // this is working correctly
        if (numberRegex.test(rule.value)) {
          rule.value = Number(rule.value);
        }
      });
    });
    console.log(rules);
    return rules;
}

现在我可以看到它正确地识别了数字并设置了值,但是当我像这样控制台结果时

console.log(checkForNumberValues(rules));

我不返回带有字符串数字值而不是我设置的数字值的规则对象。

我需要做一些特殊的事情来设置嵌套值吗?

下面是一个例子

let rules = {
  conditions: {
    any: [
      {
        all: [
          { fact: 'orange', value: '70' },
          { fact: 'apple', value: '10' },
          { fact: 'brocolli', value: '54' },
          { fact: 'kiwi fruit', value: '199' }
        ]
      }
    ]
  }
}

function checkForNumberValues(rules) {
  let numberRegex = /^(([0-9]{1,})|([0-9]{1,}\.[0-9]{1,}))$/g;
  rules.conditions.any.forEach((condition) => {
    condition.all.forEach((rule) => {
      if (numberRegex.test(rule.value)) {
         rule.value = Number(rule.value);
      }
    })
  });
  return rules;
}

console.log(checkForNumberValues(rules));

任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:1)

Regexp“记住”使用全局标志g(-> Why does a RegExp with global flag give wrong results?)时找到匹配项的最后一个索引

使用parseInt() / Number(),然后测试NaN

let rules = {
  conditions: {
    any: [
      {
        all: [
          { fact: 'orange', value: '70' },
          { fact: 'apple', value: '10' }
        ]
      }
    ]
  }
}

function checkForNumberValues(rules) {
  rules.conditions.any.forEach((condition) => {
    condition.all.forEach((rule) => {
      const val = parseInt(rule.value);
      
      if (!isNaN(val)) {
         rule.value = val;
      }
    })
  });
  return rules;
}

console.log(checkForNumberValues(rules));

答案 1 :(得分:0)

使用isNaN检查字符串是否为数字。

let rules = {
  conditions: {
    any: [
      {
        all: [
          { fact: 'orange', value: '70' },
          { fact: 'apple', value: '10' },
          { fact: 'brocolli', value: '54' },
          { fact: 'kiwi fruit', value: '199' }
        ]
      }
    ]
  }
}

function checkForNumberValues(rules) {
  rules.conditions.any.forEach((condition) => {
    condition.all.forEach((rule) => {
      if (!isNaN(rule.value)) {
         rule.value = Number(rule.value);
      }
    })
  });
  return rules;
}

console.log(checkForNumberValues(rules));

答案 2 :(得分:0)

您可以尝试其他方法

let rules = {
        conditions: {
          any: [
            {
              all: [
                { fact: 'orange', value: '70' },
                { fact: 'apple', value: '10' }
              ]
            }
          ]
        }
      }
rules.conditions.any.filter(x=>{(x.all).forEach(x=>x.value=parseInt(x.value))})
console.log(rules)