【问题标题】:Need help to make validation when two range contradict当两个范围矛盾时需要帮助进行验证
【发布时间】:2019-09-20 12:30:25
【问题描述】:

我有一个对象名称配置,其中有“from”和“to”。

const config = {
  "0": {
    id: 0,
    from: 0,
    to: 10,
    hex: null
  },
  "1": {
    id: 1,
    from: 11,
    to: 20,
    hex: null
  },
  "2": {
    id: 2,
    from: 21,
    to: 30,
    hex: null
  },
  "3": {
    id: 3,
    from: 31,
    to: 40,
    hex: null
  },
  "4": {
    id: 4,
    from: 41,
    to: 50,
    hex: null
  }
};

我必须检查现在的范围是否会相互矛盾,例如:form:0 => to:10 和 from:5=> to:20 这里第二个的从是矛盾的,因为 5 介于 0 到 10 之间

我尝试过关注但没有完全满足我的要求

function found(conf) {
  let isFound = false;
  for (let obj in conf) {
    for (let x in conf) {
      if (
        conf[obj].id !== conf[x].id &&
        (conf[x].from >= conf[obj].from && conf[x].to <= conf[obj].from)
      ) {
        console.log(conf[obj], conf[x]);
        isFound = true;
        break;
      }
    }
    if (isFound) break;
  }
  return isFound;
}
console.log(found(config));

【问题讨论】:

  • 在这种情况下,用户将通过输入更改每个 from 和 to 的值,我必须检查两个范围是否不矛盾
  • 让我换个说法。你试过什么吗? SO 是针对问题而不是针对需求。所以请尝试解决它,如果你失败了,请分享这个尝试,以便我们可以帮助你。
  • 听起来像是一个简单的双/三循环或缩减,你卡在哪里了? ps:如果用0到4作为对象键,直接用数组就行了,省了一堆字符来敲。
  • 所以... 5 只能在第一种情况下工作,为什么会有矛盾?展示你的作品!
  • 我尝试过的都在上面更新了

标签: javascript arrays json reactjs object


【解决方案1】:

通过组合所有范围创建单个数组

const arr = Object.entries(config).map(([a, b]) => b).flatMap(({from, to}) => RANGE(from, to))

其中 RANGE 是返回给定范围数组的方法:

const RANGE = (a,b) => Array.from((function*(x,y){
      while (x <= y) yield x++;
})(a,b));

然后使用以下函数在给定的 arr 中查找重复项:

function findDuplicate(array) { 
    var object = {};
    var result = [];
    array.forEach(function(item) {
      if (!object[item]) object[item] = 0;
      object[item] += 1;
    });
    for (var prop in object) {
      if (object[prop] >= 2) {
        result.push(prop);
      }
    }
    return result;
  }


const duplicates = findDuplicate(arr) 

然后最后检查duplicates.length

【讨论】:

    【解决方案2】:

    尝试重命名变量以使其有意义。

    您的逻辑是:ID 不匹配,inner 在outer 之后,但在outer 之前。

    永远不会有返回 true 的情况。

    const config = {
      "0": { id: 0, from:  0, to: 10, hex: null },
      "1": { id: 1, from: 11, to: 20, hex: null },
      "2": { id: 2, from: 21, to: 30, hex: null },
      "3": { id: 3, from: 31, to: 40, hex: null },
      "4": { id: 4, from: 41, to: 50, hex: null }
    };
    
    console.log(found(config));
    
    function found(conf) {
      for (let outer in conf) {
        for (let inner in conf) {
          let idsDontMatch = conf[outer].id !== conf[inner].id;
          let innerFromGteOuterFrom = conf[inner].from >= conf[outer].from;
          let innerToLteOuterFrom = conf[inner].to <= conf[outer].from;
          let innerAfterOuterButBeforeOuterFrom = innerFromGteOuterFrom && innerToLteOuterFrom;
          if (idsDontMatch && innerAfterOuterButBeforeOuterFrom) {
            console.log(conf[outer], conf[inner]);
            return true;
          }
        }
      }
      return false;
    }
    .as-console-wrapper { top: 0; max-height: 100% !important; }

    【讨论】:

      猜你喜欢
      • 2016-08-11
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2019-12-16
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      • 2010-10-03
      相关资源
      最近更新 更多