简体   繁体   English

JavaScript - 将数组中的每 3 个项目包装到 div

[英]JavaScript - Wrap every 3 items from an array to a div

I have an array consists 50 items.我有一个包含 50 个项目的数组。

I want to wrap every 3 items to a <div> .我想将每 3 个项目包装到一个<div>

I'm cracking my head trying to achieve this but I can't get it to work我正在努力实现这一目标,但我无法让它发挥作用


var cards = [];

for(var i=0; i < 50; i++){
    cards.push('<div class="card-item">Test</div>');
}

for(var i = 0; i < cards.length; i++){
    if(i % 3 === 0){
        let slides = populateSlide(cards[i]);
        console.log(slides);
    }
}

populateSlide = (cards) => {
    return `<div class="carousel-item item">${cards}</div>`;
}

Using the code above, I only managed to get a single item in every multiply of 3. Hence my div only has single item instead of three.使用上面的代码,我只能在 3 的每个乘法中获得一个项目。因此我的div只有一个项目而不是三个。

Thanks in advance.提前致谢。

You can store the cards in a temporary array and check its length.您可以将卡片存储在临时数组中并检查其长度。 Something like:就像是:

 { const populateSlide = cards => { console.log(cards) } const cards = [1, 2, 3, 4, 5, 6, 7, 8, 9]; let cardsTriple = []; // the temporary array cards.forEach( card => { cardsTriple.push(card); // add card to temp array if(cardsTriple.length === 3){ let slides = populateSlide(cardsTriple); cardsTriple = []; // reset the temp array } }); // there may still be cards available if (cardsTriple.length) { populateSlide(cardsTriple); } }
 .as-console-wrapper { top: 0; max-height: 100% !important; }

I'll recommand to use .slice and then .wrapAll我会建议使用.slice然后使用.wrapAll

here is an example :这是一个例子:

var divs = $("div > div");
for(var i = 0; i < cards.length; i+=3) {
  divs.slice(i, i+3).wrapAll("<div class='new'></div>");
}

Using CSS, and then directly insert all children into the parent div.使用CSS,然后将所有子项直接插入父div。 No need to do anthing in JS.无需在 JS 中做任何事情。

<div class="row" id="parent">
  <div class="col-sm-4" style="background-color:lavender;">AAA</div>
  <div class="col-sm-4" style="background-color:lavenderblush;">BBB</div>
  <div class="col-sm-4" style="background-color:lavender;">CCC</div>
  <div class="col-sm-4" style="background-color:lavender;">DDD</div>
  <div class="col-sm-4" style="background-color:lavenderblush;">EEE</div>
  <div class="col-sm-4" style="background-color:lavender;">FFF</div>
  <div class="col-sm-4" style="background-color:lavender;">AAA</div>
  <div class="col-sm-4" style="background-color:lavenderblush;">BBB</div>
  <div class="col-sm-4" style="background-color:lavender;">CCC</div>
  <div class="col-sm-4" style="background-color:lavender;">DDD</div>
  <div class="col-sm-4" style="background-color:lavenderblush;">EEE</div>
  <div class="col-sm-4" style="background-color:lavender;">FFF</div>    
</div>

cards.map(item => Document.getElementById('parent').appendChild(item));

Here is solution that's chunking items to groups of 3:这是将项目分块为 3 组的解决方案:

function chunks(array, n) {
  return Array(Math.ceil(array.length/n)).fill().map((_,i) => array.slice(i*n,i*n+n));
}

var cards = []
for(var i=0; i < 50; i++){
  cards.push('<div class="card-item">Test</div>');
}

var cardChunks = chunks(cards, 3)

var slider = cardChunks.map(cards => {
  return `<div class="carousel-item item">${cards.join('')}</div>`
}).join('')

For chunking I used solution from: https://stackoverflow.com/a/10456644/4267716 but you can find it in popular libraries like lodash.对于分块,我使用了以下解决方案: https ://stackoverflow.com/a/10456644/4267716 但您可以在 lodash 等流行库中找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM