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

Traverse nested Javascript arrays to create paths

本文关键字:创建 路径 数组 Javascript 嵌套 遍历      更新时间:2024-05-06

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

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

我想遍历这些级别来创建一个像这样的阵列阵列

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

我知道这需要递归。有什么想法吗?!

这个问题有点模糊,但这里有一个有趣的函数,它取自对象扫描,您可以根据需要进行修改。

请注意,对输入的一些假设是,您没有指定

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' ] ]
.as-console-wrapper {max-height: 100% !important; top: 0}