【问题标题】:How to ignore left part and middle element of the array(if odd array) to check existence of element in the right part?如何忽略数组的左侧和中间元素(如果是奇数数组)以检查右侧元素是否存在?
【发布时间】:2018-10-07 09:58:15
【问题描述】:

我正在尝试返回

  • true,如果项目在数组的后半部分内

  • false,如果项目在前半部分内

  • false,如果是奇数数组的中间项。

我有一些工作代码来测试这个,但是我坚持要确保奇数数组的中间项没有被测试。

function isItemPastHalf(array, item) {

  var halfWayThough = Math.floor(array.length / 2)

  var arrayFirstHalf = array.slice(0, halfWayThough);

  var arraySecondHalf = array.slice(halfWayThough, array.length);

  return arraySecondHalf.includes(item);

}

var array = [1, 2, 3];

console.log(isItemPastHalf(array, 2));

到目前为止,此代码不适用于 array = [1, 2, 3] item = 2,因为 2 是奇数的中间值,它应该返回 false。针对这个特定问题的其他测试包括偶数长度数组。有人对我可以使用什么有任何建议吗?

【问题讨论】:

  • 为什么不使用 indexOf 并与数组长度进行比较?无需实际拆分数组。
  • 不需要对数组进行切片,可以使用Array indexOf方法判断元素是否存在于后半部分。操作索引geeksforgeeks.org/javascript-array-indexof的详细信息可以参考以下链接
  • 非常感谢,我已经成功地使用 indexOf 和 Math.ceil 将我的代码缩减了很多,它可以工作了 :)

标签: javascript arrays


【解决方案1】:

Math.ceil() 将在这里解决问题,而不是 Math.floor()

function isItemPastHalf(array, item) {

  var halfWayThough = Math.ceil(array.length / 2);

  var arrayFirstHalf = array.slice(0, halfWayThough);

  var arraySecondHalf = array.slice(halfWayThough, array.length);

  return arraySecondHalf.includes(item);

}

var array = [1, 2, 3, 4, 5, 6, 7];

console.log(isItemPastHalf(array, 3));
console.log(isItemPastHalf(array, 2));
console.log(isItemPastHalf(array, 4));

【讨论】:

  • 太棒了,谢谢。我已经结合了 Math.ceil 和使用 indexOf 这意味着我现在可以用两行代码完成这个问题!
  • @slouisa 很高兴为您提供帮助 ;-)。如果此答案对您有帮助,您可以将其标记为答案 ;-)
【解决方案2】:

您可以通过先搜索索引然后决定返回什么来增强功能,如下所示

function isItemPastHalf (array, item) { 
    let idx = array.lastIndexOf(item);
    let middleIndex = Math.ceil(array.length/2)-1;
    return idx > middleIndex; 
 } 

【讨论】:

    【解决方案3】:

    你可以使用数组的findIndex方法。

    function isElementInTheSecondHalfOfTheArray(element, array) {
        const index = array.findIndex(arrayElement => arrayElement === element);
        return index > (array.length - 1) / 2; 
    }
    
    console.log( isElementInTheSecondHalfOfTheArray(1, [1, 2, 3]) );
    console.log( isElementInTheSecondHalfOfTheArray(2, [1, 2, 3]) );
    console.log( isElementInTheSecondHalfOfTheArray(3, [1, 2, 3]) );

    【讨论】:

      【解决方案4】:

      我建议采用以下方法:

      function isItemPastHalf(array, item) {
        // we find the index of the item in the array (which returns -1
        // if the item is not found) and check to see if that is greater
        // than, or equal to, the floored length of the array when divided
        // by 2; we use Math.floor() rather than Math.ceil() because
        // JavaScript arrays are zero-indexed:
        return array.indexOf(item) >= (Math.floor(array.length / 2));
      }
      
      var array = [1, 2, 3];
      
      console.log(isItemPastHalf(array, 2));

      [JS Fiddle 演示](https://jsfiddle.net/davidThomas/cp6vLmsy/.

      参考资料:

      【讨论】:

        【解决方案5】:

        let numbers = [1, 2, 3, 4, 5]
        
        function isItemPastHalf(array, item){
          if((array.indexOf(item)*2) > array.length)
            return true;
            return false
        }
        
        console.log(isItemPastHalf(numbers,3)) //false if it is the middle item

        如果项的索引乘以2仍然小于数组长度,则该项必须在前半部分。

        【讨论】:

          【解决方案6】:

          function isItemPastHalf(array, item) {
            let itemIndex = array.lastIndexOf(item)
            let mid = Math.floor(array.length/2)
            return mid <  itemIndex;
          }
          
          var oddLengthArray = [1, 2, 3, 4, 5, 6, 7];
          var evenLengthArray = [1, 2, 3, 4, 5, 6];
          
          console.log("ODD LENGTH ARRAY")
          console.log("3",isItemPastHalf(oddLengthArray, 3));
          console.log("4",isItemPastHalf(oddLengthArray, 4));
          console.log("5",isItemPastHalf(oddLengthArray, 5));
          
          console.log("ODD LENGTH ARRAY")
          console.log("3",isItemPastHalf(evenLengthArray, 3));
          console.log("4",isItemPastHalf(evenLengthArray, 4));
          console.log("5",isItemPastHalf(evenLengthArray, 5));

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-07-02
            • 2021-11-11
            • 2010-12-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-11-07
            相关资源
            最近更新 更多