首页 > 解决方案 > 查找多个数组中的连续数字 JavaScript

问题描述

我的数组结构如下:

[
    [8,[1]],
    [15,[2]],
    [20,[3]],
    [23,[4,41]],
    [497,[18]],
    [1335,[38]],
    [2092,[39,55,61]],
    [3615,[5]],
    [4121,[14]],
    [5706,[39,55,61]],
    [5711,[62]],
    [5714,[63]],
    [5719,[64]],
    [6364,[38]]
]

我使用此答案中的修改后的代码来查找连续数字,但我无法调整它以从具有多个值的数组中查找连续数字

这是我的代码:

const a = [
    [8,[1]],
    [15,[2]],
    [20,[3]],
    [23,[4,41]],
    [497,[18]],
    [1335,[38]],
    [2092,[39,55,61]],
    [3615,[5]],
    [4121,[14]],
    [5706,[39,55,61]],
    [5711,[62]],
    [5714,[63]],
    [5719,[64]],
    [6364,[38]]
];

// this variable will contain arrays
let finalArray = [];
// Create a recursive function
function checkPrevNextNumRec(array) {
  let tempArr = [];
  // if the array contaon only 1 element then push it in finalArray and
  // return it
  if (array.length === 1) {
    finalArray.push(array);
    return
  }
  // otherside check the difference between current & previous number
  for (let i = 1; i < array.length; i++) {
    if (array[i][1][0] - array[i - 1][1][0] === 1) {
      // if current & previous number is 1,0 respectively 
      // then 0 will be pushed
      tempArr.push(array[i - 1]);
    } else {
      // if current & previous number is 5,2 respectively 
      // then 2 will be pushed
      tempArr.push(array[i - 1])
      // create a an array and recall the same function
      // example from [0, 1, 2, 5, 6, 9] after removing 0,1,2 it 
      // will create a new array [5,6,9]
      let newArr = array.splice(i);
      finalArray.push(tempArr);
      checkPrevNextNumRec(newArr)
    }
    // for last element if it is not consecutive of
    // previous number 
    if (i === array.length - 1) {
      tempArr.push(array[i]);
      finalArray.push(tempArr)
    }
  }
  }
checkPrevNextNumRec(a)

如您所见,这里的结果,所有包含连续数字的表格[i][1][0]都已分组

[
    [
        [8,[1]],
        [15,[2]],
        [20,[3]],
        [23,[4,41]]
    ],
    [
        [497,[18]]
    ],
    [
        [1335,[38]],
        [2092, [39,55,61]]
    ],
    [
        [3615,[5]]
    ],
    [
        [4121,[14]]
    ],
    [
        [5706,[39,55,61]]
    ],
    [
        [5711,[62]],
        [5714,[63]],
        [5719,[64]]
    ],
    [
        [6364,[38]]
    ]
]

但我需要字段 5706 也包含在 5711、5714 和 5719 中,但显然它不包括在内,因为[i][1][0] 我没有想到受到这篇文章的启发,但我无法正确集成它

你能帮助我吗?

谢谢!

标签: javascriptarrays

解决方案


基本上这里的逻辑是迭代input和 在每次迭代时,将其与前一次迭代的数据值进行比较,即:prevData

在每次迭代中,我Array.prototype.map通过简单地将 1 中的每个项目添加到prevData.

下一步是循环该数组以查看我们是否遇到连续值 - 只要我们可以break,因为不需要继续检查。

最后是我们将分组逻辑与单个if/else.

const a = [
  [8, [1]],
  [15, [2]],
  [20, [3]],
  [23, [4, 41]],
  [497, [18]],
  [1335, [38]],
  [2092, [39, 55, 61]],
  [3615, [5]],
  [4121, [14]],
  [5706, [39, 55, 61]],
  [5711, [62]],
  [5714, [63]],
  [5719, [64]],
  [6364, [38]]
];

function group(input) {
  let prev;

  return input.reduce((accum, el) => {
    let hasConsecutive = false;
    const [key, current] = el;

    if (prev == null) {
      prev = current;
    }

    const consecutives = prev.map(n => n + 1);

    for (let i = 0; i < consecutives.length; i += 1) {
      if (current.includes(consecutives[i])) {
        hasConsecutive = true;
        break;
      }
    }

    if (prev && hasConsecutive) {
      accum[accum.length - 1].push(el);
    } else {
      accum.push([el]);
    }

    prev = current;

    return accum;
  }, []);
}

console.log(group(a));

这是通过美化器运行的结果:

[
    [
        [8, [1]],
        [15, [2]],
        [20, [3]],
        [23, [4, 41]]
    ],
    [
        [497, [18]]
    ],
    [
        [1335, [38]],
        [2092, [39, 55, 61]]
    ],
    [
        [3615, [5]]
    ],
    [
        [4121, [14]]
    ],
    [
        [5706, [39, 55, 61]],
        [5711, [62]],
        [5714, [63]],
        [5719, [64]]
    ],
    [
        [6364, [38]]
    ]
]

推荐阅读