数组返回元素,其中值介于其他值

时间:2017-12-21 08:12:00

标签: javascript arrays typescript multidimensional-array

我有一个数组[{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或打字稿中检查?

3 个答案:

答案 0 :(得分:0)

试试这个。您可以添加任何值。然后你需要获得独特的价值而不是添加。像这样下面的方式。



 

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);




答案 1 :(得分:0)

您需要执行以下步骤:

  1. 循环播放数组。
  2. 检查新项目是 inBetween 范围。 (我为你写了一个函数,请在下面查看,你可以用它来检查值是否在指定范围之间。)
  3. 如果上面的检查返回false,则添加到数组。 (或做任何你想做的事。)
  4. 请在循环内使用下面的函数来迭代数组。

    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 :(得分:0)

在以下代码段中:shouldpush()测试object pushed是否应array shouldpush()(根据您的规则)。

returns resulting array [bool, newarray](按顺序),格式为result

您可以这样收集const [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.

&#13;
&#13;
render()
 { 
    return (
        <div>
            <Child
                value={this.props.inputValue}                    
                onChange={this.update}
            />
        </div>
  }

      Child render
           <input
                value={this.props.inputValue}                    
                onChange={this.props.update}
            />
        </div>
&#13;
&#13;
&#13;