遍历嵌套的Javascript数组以创建路径

时间:2013-12-06 21:24:05

标签: javascript recursion multidimensional-array mutual-recursion

我有一个像这样的任意嵌套数据结构 -

var nested = [ 'a', [ [ 'b' ], [ 'c' ] ] ];

我想遍历各个级别来创建一个像这样的数组数组

var paths = [['a', 'b'] ['a', 'c']];

我知道这将采取递归。有什么想法吗?!

1 个答案:

答案 0 :(得分:0)

问题有点模糊,但这是从object-scan提取的一个有趣的函数,您可以根据需要进行修改。

请注意,一些关于输入的假设是您未指定的

const iterate = (tree, cb) => {
  const stack = [tree];
  const parent = [null];
  const count = [];
  const depth = [];
  const path = [];
  let idx = 0;
  let inc = true;

  while (idx !== -1) {
    const e = stack[idx];
    if (e instanceof Set) {
      stack[idx] = [...e];
      stack[idx].or = true;
    } else if (Array.isArray(e)) {
      if (e.or !== true) {
        stack.splice(idx, 1, ...e);
        parent.splice(idx, 1, ...new Array(e.length).fill(parent[idx]));
        if (parent[idx] !== null) {
          depth[parent[idx]] += e.length - 1;
        }
      } else {
        if (count[idx] === undefined) {
          count[idx] = 0;
          depth[idx] = 0;
        } else if (depth[idx] !== 0) {
          stack.splice(idx + 1, depth[idx]);
          parent.splice(idx + 1, depth[idx]);
          depth[idx] = 0;
        }

        if (count[idx] < e.length) {
          stack.splice(idx + 1, 0, e[count[idx]]);
          parent.splice(idx + 1, 0, idx);
          count[idx] = (count[idx] || 0) + 1;
          depth[idx] += 1;
          inc = true;
          idx += 1;
        } else {
          count[idx] = 0;
          idx -= 1;
        }
      }
    } else if (inc === true) {
      path.push(e);
      cb('ADD', e);
      if (idx === stack.length - 1) {
        cb('FIN', path);
        inc = false;
      } else {
        idx += 1;
      }
    } else {
      cb('RM', path.pop());
      idx -= 1;
    }
  }
};

const extract = (tree) => {
  const result = [];
  iterate(tree, (type, p) => {
    if (type === 'FIN') {
      result.push([...p]);
    }
  });
  return result;
};

console.log(extract(['a', new Set([['b'], ['c']])]));
// => [ [ 'a', 'b' ], [ 'a', 'c' ] ]

console.log(extract(['a', new Set([['b', new Set(['d', 'e'])], ['c']])]));
// => [ [ 'a', 'b', 'd' ], [ 'a', 'b', 'e' ], [ 'a', 'c' ] ]

console.log(extract(['a', new Set([['b'], ['c']]), 'd']));
// => [ [ 'a', 'b', 'd' ], [ 'a', 'c', 'd' ] ]