【问题标题】:Array return elements where values falls between other values数组返回值介于其他值之间的元素
【发布时间】:2018-06-03 20:58:34
【问题描述】:

我有一个数组[{lowlimit: 0, highlimit 100}, {lowlimit: 201, highlimit: 300}]

应接受新元素 {lowlimit: 301, highlimit: 400}{lowlimit: 101, highlimit: 150}

但不应接受像 {lowlimit: 50, highlimit: 80}{lowlimit: 50, highlimit: 250} 这样的元素,因为它在现有元素之一内或与现有范围重叠

有什么建议可以在 javascript 或 typescript 中检查吗?

【问题讨论】:

  • 1.遍历数组 2。检查新项目是否重叠。 3.如果没有则添加到数组中
  • 3.编写自己的代码
  • 4.在你写完代码后在这里发布问题,你仍然遇到问题

标签: javascript arrays typescript multidimensional-array


【解决方案1】:

您需要遵循一些步骤:

  1. 遍历数组。
  2. 检查新项目是否在在Between范围内。 (我为你写了一个函数,在下面检查它,你可以用它来检查值是否在指定范围内。)
  3. 如果上述检查返回 false,则添加到数组。 (或随心所欲)。

请在遍历数组的循环内使用下面的函数。

function _isBetweenRange(a, b){
    var aLowLimit = a.lowlimit;
  var aHighLimit = a.highlimit;
  var bLowLimit = b.lowlimit;
  var bHighLimit = b.highlimit;
  if(bLowLimit >= aLowLimit && bHighLimit <= aHighLimit){
    return true;
  }else{
    return false;
  }
}

【讨论】:

【解决方案2】:

试试这个。您可以添加任何值。然后你需要获得唯一的价值而不是同时添加。像下面这样。

 

var a = [{lowlimit: 0, highlimit : 100}, {lowlimit: 201, highlimit:300}, 
     {lowlimit: 50, highlimit: 300}, {lowlimit: 50, highlimit: 300}];

var elementId = [];

var newArr = a.filter(function(el, index){
if (elementId.indexOf(el.lowlimit) === -1) {
    elementId.push(el.lowlimit);
    return true;
} else {
    return false;
}
});
console.log(newArr);

【讨论】:

    【解决方案3】:

    在下面的 sn-p:shouldpush() 中测试 object 是否应该是 pushedarray(根据您的规则)。

    shouldpush()returnsresultingarray(按顺序),格式为[bool, newarray]

    您可以像这样收集resultconst [push, newarray] = shouldpush(array, object)

    // Array.
    const array = [{lowlimit: 0, highlimit: 100}, {lowlimit: 201, highlimit: 300}]
    
    // Should Push.
    const shouldpush = (array, object) => {
    
      const length = array.length
    
      for (let i = 0; i <= (length-1); i ++) {
    
        // Comps.
        const pre = array[i] || false
        const post = array[i+1] || false
    
        // Cases:
        
        // Length(1) AND Fit Above (Extreme Edge Case).
        if ((length == 1) && (pre.highlimit < object.lowlimit)) return [true, [...array, object]]
        
        // First Fit (Below).
        const atfirst = (i == 0) // At First Index.
        const firstfit = (atfirst && (pre.lowlimit > object.highlimit)) // First Fit.
        if (firstfit) return [true, [object, ...array]] // True.
        
        // Normal Fit.
        const lowfit = (object.lowlimit > pre.highlimit) // Standard Low.
        const highfit = (object.highlimit < post.lowlimit) // Standard High.
        if (lowfit && highfit) return [true, [...array.slice(0, i+1), object, ...array.slice(i+1)]]
        
        // Last Fit (Above).
        const atlast = (i == (length - 2)) // At Last Index.
        const lastfit = (atlast && (post.highlimit < object.lowlimit)) // Last Fit.
        if (lastfit) return [true, [...array, object]] // True.
        
        // No Fit.
        if (!lowfit && !highfit) return [false, [...array]] // !Fit (Early Exit).
      }
      
      return [false, [...array]] // !Fit.
    }
    
    // Test Objects.
    const w = {lowlimit: 301, highlimit: 400} // W.
    console.log(shouldpush(array, w)) // True.
    const x = {lowlimit: 101, highlimit: 150} // X.
    console.log(shouldpush(array, x)) // True.
    const y = {lowlimit: 50, highlimit: 80} // Y.
    console.log(shouldpush(array, y)) // False.
    const z = {lowlimit: 50, highlimit: 250} // Z.
    console.log(shouldpush(array, z)) // False.
    
    // Example Retrieval.
    const [push, newarray] = shouldpush(array, x)
    console.log(push, newarray) // Push. New Array.

    【讨论】:

    • 谢谢,效果很好,我看到的唯一问题是循环从 1 开始,所以如果数组中只有 2 个对象,则 const post 未定义。
    • 我明白你的意思了。不太清楚为什么我从i = 1 开始。刚刚重构为从0 开始。定义了一种更好的方法来覆盖边缘情况,例如length == 1。感谢您的反馈。修改后的解决方案见上文。
    猜你喜欢
    • 2021-09-29
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    相关资源
    最近更新 更多