【问题标题】:Get the N elements from array based on the position根据位置从数组中获取N个元素
【发布时间】:2017-03-24 12:33:50
【问题描述】:

我想要一个函数,它返回一个位置和编号的子数组。我想要的元素。我认为可能有一些算法可以找到枢轴点或其他东西&从中我可以得到子数组,但我完全忘记了。

Example: a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
I want 6 elements
if position = 0, then I want [1, 2, 3, 4, 5, 6]
if position = 1, then [1, 2, 3, 4, 5, 6]
if position = 2, then [1, 2, 3, 4, 5, 6]
if position = 3, then [1, 2, 3, 4, 5, 6]
if position = 4, then [2, 3, 4, 5, 6, 7]
if position = 5, then [3, 4, 5, 6, 7, 8]
if position = 6, then [4, 5, 6, 7, 8, 9]
if position = 7, then [5, 6, 7, 8, 9, 10]
if position = 8, then [5, 6, 7, 8, 9, 10]
if position = 9, then [5, 6, 7, 8, 9, 10]
simply get the middle of N elements based on the position I pass.

我可以编写自己的loop,其中将包含多个if-else 条件来完成它。但我觉得可能有一些简单的方法可以做到这一点。

我没有包含我不完整的代码 sn-p,因为我强烈认为必须有一些算法来做到这一点。

【问题讨论】:

  • 仓位是怎么用的?为什么指定位置为 4 时跳过第一个元素?
  • 这就像,如果我的位置是数组的中心位置,让我得到具有 N 个元素的中间数组。有意义吗
  • 好的,我错过了,我得到了一个起始索引和最小长度。我会相应地更新我的 sn-p。 :)

标签: javascript arrays algorithm


【解决方案1】:

你想要的是:Array.prototype.slice(...)

这里有很好的记录:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

var n = 6;
var start = Math.max(0, Math.min(Math.floor(position-n/2), a.length-n));
return a.slice(start, start+n);

【讨论】:

  • 根据您在原始问题中的评论更新。
【解决方案2】:

简单的方法:

var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

function getSubArray(idx, _length, _array) {
  return _array.slice(idx, idx + _length);
}

var subArray = getSubArray(3, 6, a);

【讨论】:

  • 它工作得很好,但是getSubArray(5, 6, a) => [6, 7, 8, 9, 10] ,我希望[4,5,6,7,8,9] or [3,4,5,6,7,8] 从位置 5 看起来是中间的。
  • 嘿@LonelyPlanet,我们的目标是通过初始首发位置,不是吗?然后,计算前向项目。如果你想返回,[4,5,6,7,8,9],你应该打电话给getSubArray(3, 6, a)
【解决方案3】:

您可以对位置使用偏移量,并首先获取起始值进行切片。

var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    n = 6,
    i, 
    start;

for (i = 1; i < 12; i++) {
    start = Math.max(Math.min(i - n / 2, a.length - n), 0);
    console.log(i, ': ', a.slice(start, start + n).join());      
}

【讨论】:

    【解决方案4】:

    您唯一需要的是检查您是否不会检查不存在的 pos。喜欢:

    var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    var n = 6; // Number of result you want
    var x = 8; // Pos you want
    
    // If you gonna exceed your length, we got only the n last element
    if((x+(n/2)) > a.length) { 
        console.log(a.slice(a.length-n)); 
    // Otherwise, if under 0, we got the n first
    } else 
        if((x-(n/2)) < 0) { console.log(a.slice(0,n) ); 
    // Default case
        } else { 
    console.log(a.slice((x-(n/2)),(x+(n/2))));
    }
    

    这不是最聪明的方法,但他可以给你一些提示。我像其他提到的那样使用切片来避免很多 if,但你应该做 GENERIC 测试。

    【讨论】:

      【解决方案5】:

      类似这样的:

      a = [1,2,3,4,5,6,7,8,9,10];
      n = 6;
      function split(position) {
          var start = Math.min(Math.max(position - Math.floor(n/2), 0), a.length - n);
          var stop = Math.min(start+n, a.length);
          return a.slice(start, stop);
      }
      

      【讨论】:

        【解决方案6】:

        根本不需要 Math 对象。您可以简单地执行以下操作;

        function getArr(a,n,d){
          n = n - 4 < 0 ? 0
                        : a.length - d > n - 4  ? n - 3
                                                : a.length - d;
          return a.slice(n,n + d);
        }
        
        var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
           diff = 6;
        for (var i = 0; i < 10; i ++) console.log(JSON.stringify(getArr(arr,i,diff)));

        【讨论】:

          【解决方案7】:

          不需要 if-else,您可以使用 arr[position] 到 arr[8]。你有吗

          function getArr(arr,position,requiredNumbers){
          return arr.slice(position, position+requiredNumbers);
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-04-30
            • 1970-01-01
            • 2021-12-22
            • 2016-04-25
            • 1970-01-01
            • 1970-01-01
            • 2019-10-18
            相关资源
            最近更新 更多