我有一组通过WordPress Ajax填充的JSON数据。
["http://inovar.dev/wp-content/uploads/2016/11/FR991-Eco-Oak-A1.jpg",…]
目前,项目总数为69。如何将每组分为18个项目。然后像这样包装它:
<div class="slide">
<a class="floor-thumbnail"><img src="/wp-content/uploads/2017/01/BG_2-3.jpg" alt=""/></a>
<a class="floor-thumbnail"><img src="/wp-content/uploads/2017/01/BG_2-3.jpg" alt=""/></a>
<a class="floor-thumbnail"><img src="/wp-content/uploads/2017/01/BG_2-3.jpg" alt=""/></a>
<a class="floor-thumbnail"><img src="/wp-content/uploads/2017/01/BG_2-3.jpg" alt=""/></a>
<a class="floor-thumbnail"><img src="/wp-content/uploads/2017/01/BG_2-3.jpg" alt=""/></a>
<a class="floor-thumbnail"><img src="/wp-content/uploads/2017/01/BG_2-3.jpg" alt=""/></a>
最佳答案
var urls = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22];
var href = function (url) {
return '<a class="floor-thumbnail"><img src="' + url + '" alt=""/></a>\r\n'
}
var groupLimit = 18;
var output = urls.reduce(function (htmlString, current, index) {
var mod = index %groupLimit;
if (mod === 0) {
if (index >= groupLimit) htmlString += '</div>\r\n';
htmlString += '<div class="slide">\r\n'
}
return htmlString + href(current)
}, '') + '</div>';
console.log(output)
使用reduce和the remainder operator