遍历如何按根节点项的顺序嵌套 JSON

How Traverse nested JSON in order with root node items

我有一个嵌套的JSON结构,就像一个播放列表可以有图片,视频,这个播放列表也可以有另一个嵌套的播放列表。

所以我希望得到输出,就像我从头到尾遍历最顶层的播放列表一样,得到它 nested/child 播放列表的项目只是落在最顶层循环索引中的那个。

示例一:输入结构及其预期输出:

示例二:输入结构及其预期输出:

到目前为止我已经试过了

<html>
<head>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
    <script>

        $.getJSON( "https://5da7520623fa7400146975dd.mockapi.io/api/playlist", function(data){
            iterate(data.contents, 3)
        });


        //*******response is received and passed, all your logic goes here*********
        function iterate(contents, maxRound) {
            for(i = 1; i <= maxRound; i++) {
                parse(contents,i)
            }
        }

        function parse(contents, cycle) {
            $.each(contents, function(i, content) {
                if(content.type == "playlist") {
                    var contentsLength = content.contents.length
                    var indexForRound = cycle % contentsLength
                    if(contentsLength == cycle) {
                        indexForRound = contentsLength - 1
                    }else {
                        indexForRound = indexForRound - 1
                    }
                    const onlyContentToChild = content.contents[indexForRound]
                    parse([onlyContentToChild], 1)
                }else {
                    $("#contents").append('<li> '+ content.name +' </li>');
                }
            });
        }
    </script>
</head>
    <body>
        <ol id="contents">
        </ol>
    </body>
</html>

注意:API 的调用即 get playlist returns 响应匹配示例二

这是一个简单的递归解决方案

const myData = { contents: [{ name: 'image 1', type: 'file' }, { name: 'playlist 1', type: 'playlist', level: 1, contents: [{ name: 'image 3', type: 'file' }, { name: 'playlist 101', type: 'playlist', level: 2, contents: [{ name: 'image 101', type: 'file' }, { name: 'image 102', type: 'file' }, { name: 'image 103', type: 'file' }, { name: 'image 104', type: 'file' }] }, { name: 'image 4', type: 'file' }] }, { name: 'image 2', type: 'file' }, { name: 'playlist 2', type: 'playlist', level: 1, contents: [{ name: 'image 5', type: 'file' }, { name: 'image 6', type: 'file' }, { name: 'image 7', type: 'file' }] }] };

const Iterator = (data) => {
  const state = { idx: -1 };
  const next = (curData, curState) => {
    if (curData.type === 'file') {
      return curData.name;
    }
    if (!('contents' in curState)) {
      curState.contents = Array.from(
        { length: curData.contents.length },
        () => ({ idx: -1 })
      );
    }
    curState.idx = (curState.idx + 1) % curData.contents.length;
    return next(curData.contents[curState.idx], curState.contents[curState.idx]);
  };
  return () => Array.from(
    { length: data.contents.length },
    () => next(data, state)
  );
};

const next = Iterator(myData);
for (let idx = 0; idx < 13; idx += 1) {
  console.log(next());
}
// => [ 'image 1', 'image 3', 'image 2', 'image 5' ]
// => [ 'image 1', 'image 101', 'image 2', 'image 6' ]
// => [ 'image 1', 'image 4', 'image 2', 'image 7' ]
// => [ 'image 1', 'image 3', 'image 2', 'image 5' ]
// => [ 'image 1', 'image 102', 'image 2', 'image 6' ]
// => [ 'image 1', 'image 4', 'image 2', 'image 7' ]
// => [ 'image 1', 'image 3', 'image 2', 'image 5' ]
// => [ 'image 1', 'image 103', 'image 2', 'image 6' ]
// => [ 'image 1', 'image 4', 'image 2', 'image 7' ]
// => [ 'image 1', 'image 3', 'image 2', 'image 5' ]
// => [ 'image 1', 'image 104', 'image 2', 'image 6' ]
// => [ 'image 1', 'image 4', 'image 2', 'image 7' ]
// => [ 'image 1', 'image 3', 'image 2', 'image 5' ]
.as-console-wrapper {max-height: 100% !important; top: 0}

这里是动态终止的更新版本

const myData = { contents: [{ name: 'image 1', type: 'file' }, { name: 'playlist 1', type: 'playlist', level: 1, contents: [{ name: 'image 3', type: 'file' }, { name: 'playlist 101', type: 'playlist', level: 2, contents: [{ name: 'image 101', type: 'file' }, { name: 'image 102', type: 'file' }, { name: 'image 103', type: 'file' }, { name: 'image 104', type: 'file' }] }, { name: 'image 4', type: 'file' }] }, { name: 'image 2', type: 'file' }, { name: 'playlist 2', type: 'playlist', level: 1, contents: [{ name: 'image 5', type: 'file' }, { name: 'image 6', type: 'file' }, { name: 'image 7', type: 'file' }] }] };

const iterate = (data) => {
  const state = { idx: -1 };
  const progress = { cur: 0, max: 0 };
  const next = (curData, curState) => {
    if (curData.type === 'file') {
      return curData.name;
    }
    const length = curData.contents.length;
    if (!('contents' in curState)) {
      curState.contents = Array.from({ length }, () => ({ idx: -1 }));
      progress.max += length;
    }
    if (curState.idx === length - 1) {
      progress.cur -= curState.idx;
      curState.idx = 0;
    } else {
      progress.cur += 1;
      curState.idx += 1;
    }
    return next(curData.contents[curState.idx], curState.contents[curState.idx]);
  };
  const nextBatch = () => Array.from(
    { length: data.contents.length },
    () => next(data, state)
  );

  const result = [];
  do {
    result.push(nextBatch());
  } while (progress.cur !== progress.max);
  return result;
};

console.log(iterate(myData));
/* => [
  [ 'image 1', 'image 3', 'image 2', 'image 5' ],
  [ 'image 1', 'image 101', 'image 2', 'image 6' ],
  [ 'image 1', 'image 4', 'image 2', 'image 7' ],
  [ 'image 1', 'image 3', 'image 2', 'image 5' ],
  [ 'image 1', 'image 102', 'image 2', 'image 6' ],
  [ 'image 1', 'image 4', 'image 2', 'image 7' ],
  [ 'image 1', 'image 3', 'image 2', 'image 5' ],
  [ 'image 1', 'image 103', 'image 2', 'image 6' ],
  [ 'image 1', 'image 4', 'image 2', 'image 7' ],
  [ 'image 1', 'image 3', 'image 2', 'image 5' ],
  [ 'image 1', 'image 104', 'image 2', 'image 6' ],
  [ 'image 1', 'image 4', 'image 2', 'image 7' ]
] */
.as-console-wrapper {max-height: 100% !important; top: 0}

如果不存在 filetype,这可能会严重崩溃。

这里有一个不同的方法:

const makeFn = (fns, i = -1) => () => 
  fns [i = (i + 1) % fns .length] ()

const makeHandler = ({type, name, contents}) => 
  type == 'playlist'
    ? makePlaylistFn (contents)
    : () => name

const makePlaylistFn = (contents) => 
  makeFn (contents .map (makeHandler)) 

const makePlaylist = ({contents}, play = makePlaylistFn (contents)) => (count) => 
  Array .from (
    {length: count}, 
    () => Array .from ({length: contents .length}, play)
  )

const data = {contents: [{name: "image 1", type: "file"}, {name: "playlist 1", type: "playlist", level: 1, contents: [{name: "image 3", type: "file"}, {name: "playlist 101", type: "playlist", level: 2, contents: [{name: "image 101", type: "file"}, {name: "image 102", type: "file"}, {name: "image 103", type: "file"}, {name: "image 104", type: "file"}]}, {name: "image 4", type: "file"}]}, {name: "image 2", type: "file"}, {name: "playlist 2", type: "playlist", level: 1, contents: [{name: "image 5", type: "file"}, {name: "image 6", type: "file"}, {name: "image 7", type: "file"}]}]}

const myPlaylist = makePlaylist (data)

console .log (myPlaylist (12))
.as-console-wrapper {max-height: 100% !important; top: 0}

为了了解这是如何工作的,我们可以想象如果我们将数据转换成这种格式会是什么样子:

const foo = ((i) => {
  const fns = [
    () => 3,
    () => 4,
  ]
  return () => fns[i = (i + 1) % fns.length]()
})(-1)

const bar = ((i) => {
  const fns = [
    () => 5,
    () => 6,
    () => 7,
  ]
  return () => fns[i = (i + 1) % fns.length]()
})(-1)

const baz = ((i) => {
  const fns = [
    () => 1,
    foo,
    () => 2,
    bar,
  ]
  return () => fns[i = (i + 1) % fns.length]()
})(-1)

const qux = () => Array.from({length: 4}, baz)

console .log (
  Array .from ({length: 6}, qux)
)
.as-console-wrapper {max-height: 100% !important; top: 0}

我们的输出只是一个数组,由对 qux 的六个单独调用组成。但是 qux 只对 baz 进行了四次调用。这是它变得更有趣的地方。 baz 循环执行四个函数,每次调用返回下一个函数的结果。这四个函数是 () => 1foo() => 2barfoobar 的结构与 foo 使用两个函数 () => 3() => 4 以及 bar 使用三个函数 () => 5 的结构相同]、() => 6() => 7

所以上面的代码就是为了把你的数据结构变成这样一个嵌套函数的列表。 Central 是 makePlaylistFn,它创建了我们的主要功能(相当于上面的 baz,通过将我们的值映射到 makeHandler,它区分了 "playlist""file" 输入,递归地为前者回调 makePlaylistFn 并为后者返回一个简单的函数。结果函数被传递给 makeFn,它将一个函数数组变成一个在每次调用时循环调用它们的函数。

我们最后用 makePlayList 结束,它调用 makePlaylistFn 来生成那个根函数,returns 一个接受正整数的函数,返回那么多数组 n 调用主函数,其中 n 是最外层播放列表的长度。


这还留下一个有趣的问题。我们调用这个函数多少次?您上面的示例似乎表明您希望在看到每个值后停止。我相信我们可以解决这个问题,尽管这可能很棘手。

另一种可能性是 运行 直到您循环遍历所有内容并重新开始。这更容易处理,在各种嵌套播放列表上使用最小公倍数技术。但是,对于您的简单案例,它需要调用 6 次,对于更复杂的案例,则需要调用 12 次。如果你有兴趣,我可以试一试。

更新

您想知道如何循环播放列表的值。 makePlaylist 的替代品应该这样做:

const gcd = (a, b) => 
  (b > a) ? gcd (b, a) : b == 0 ? a : gcd (b, a % b)
const lcm = (a, b) => 
  a / gcd (a, b) * b
const lcmAll = (ns) => 
  ns .reduce (lcm, 1)

const period = (contents) => lcmAll (
  [contents .length, ... contents .map (({type, name, contents}) => 
    type == 'playlist' ? period (contents) : 1
  )]
)

const makePlaylist = ({contents}) => {
  const f1 = makePlaylistFn (contents)
  const f2 = () => Array .from ({length: contents .length}, f1)
  return Array .from ({length: period (contents)}, f2)
}

我们从三个需要找到重复周期的数学函数开始。我们需要找到整数数组的最小公倍数。这就是 lcmAll 的含义 例如,lcmAll([12, 15, 20, 35]) 产生 420,这些整数的最小公倍数。它是作为我们的最小公倍数函数 lcm 的简单缩减构建的,后者又构建在最大公约数 gcd 函数之上。

使用它,我们在 period 中递归地找到每个 images/playlists 的 LCM。最后,我们重写 makePlaylist 以使用它来确定要创建多少个数组。其余的将保持不变。您可以通过展开此代码段来查看它的实际效果:

// utility functions
const gcd = (a, b) => 
  (b > a) ? gcd (b, a) : b == 0 ? a : gcd (b, a % b)
const lcm = (a, b) => 
  a / gcd (a, b) * b
const lcmAll = (ns) => 
  ns .reduce (lcm, 1)


// helper functions
const makeFn = (fns, i = -1) => () => 
  fns [i = (i + 1) % fns .length] ()

const makeHandler = ({type, name, contents}) => 
  type == 'playlist'
    ? makePlaylistFn (contents)
    : () => name

const period = (contents) => lcmAll (
  [contents .length, ... contents .map (({type, name, contents}) => 
    type == 'playlist' ? period (contents) : 1
  )]
)

const makePlaylistFn = (contents) => 
  makeFn (contents .map (makeHandler)) 


// main function
const makePlaylist = ({contents}) => {
  const f1 = makePlaylistFn (contents)
  const f2 = () => Array .from ({length: contents .length}, f1)
  return Array .from ({length: period (contents)}, f2)
}


// sample data
const data = {contents: [{name: "image 1", type: "file"}, {name: "playlist 1", type: "playlist", level: 1, contents: [{name: "image 3", type: "file"}, {name: "playlist 101", type: "playlist", level: 2, contents: [{name: "image 101", type: "file"}, {name: "image 102", type: "file"}, {name: "image 103", type: "file"}, {name: "image 104", type: "file"}]}, {name: "image 4", type: "file"}]}, {name: "image 2", type: "file"}, {name: "playlist 2", type: "playlist", level: 1, contents: [{name: "image 5", type: "file"}, {name: "image 6", type: "file"}, {name: "image 7", type: "file"}]}]}


// demo
console.log (makePlaylist (data))
.as-console-wrapper {max-height: 100% !important; top: 0}