我想知道这个标题是否正确,
不管怎样,我想知道的是,怎样才能做到这样的事情?
const items = [a1,a2,a3,a4,a5,a6,a7,a8,a9 .. and so on ]
const flexbox1 = [];
const flexbox2 = [];
const flexbox3 = [];
const flexbox4 = [];
for (const item in items) {
// what I wonder is this part.
}
==> result:
const flexbox1 = [a1,a5,a9,a13 .. and so on];
const flexbox2 = [a2,a6,a10,a14 .. and so on];
const flexbox3 = [a3,a7,a11,a15 .. and so on];
const flexbox4 = [a4,a8,a12,a16 ... and so on];我怎样才能得到上面的结果?
两天来,我一直试图在书中和网上找到答案,但我找不到答案。
请帮忙..。
提前谢谢。
发布于 2020-03-13 13:59:09
我认为这不是最干净的解决方案,但它应该有效:)
const items = [...Array(20).keys()];
let currentIndex = 1;
const data = {
flexbox1: [],
flexbox2: [],
flexbox3: [],
flexbox4: []
};
for (const item in items) {
data[`flexbox${currentIndex}`].push(item);
currentIndex = currentIndex === 4 ? 1 : currentIndex + 1;
}
console.log(data);
https://stackoverflow.com/questions/60671710
复制相似问题