0

I'm retrieving records from a .json file, that I'm generating with node.js (with fs, ejs, http, request, connect modules) ;

That's the code I'm using to retrieve the records:

<% layers.forEach(function(layer) { %>
<div class="dr-container" style="-webkit-transform: rotate(<%= layer.angle %>deg);">
<% for (j = 1; j <= layer.repeat; j++) { %>
<div class="dr-box" style="height: <%= 100 / layer.repeat %>%;background-image: url('data:image/svg+xml;charset=utf-8,<%= layer.linetype %><%= layer.swatch %>');"></div><% } %></div>
<% }); %>

(layers is the array I'm pulling info from ; angle, repeat, linetype, swatch are the fields from the input form on the template. The loop inside the div is meant to repeat the same result a certain number of times and it's working well)

What can I do to retrieve the remainder of all records divided by 6, and show only those?

Like having 38 records and showing the last 2. I understood I need a loop before the 1st line that triggers that?

4

2 回答 2

1

我一直在寻找可以给我同样东西的东西,但不仅使用数组中的索引,而且使用可以无限大的任意位置(即+100 - 100),所以在这种情况下,我只是在上面的代码中添加了一行-12。如果有人需要它。

function carousel(array, arbitraryIndex, n) {
var result = [];
index = arbitraryIndex % (array.length);
for (var i = index - n, len = array.length; i <= index + n; i++) {
    result.push(array[i < 0 ? len + i : i > len - 1 ? i - len : i]);
}

return result;
}

var array = [0, 1, 2, 3, 4, 5],
arbitraryIndex = -12,
n = 3;

console.log(carousel(array, arbitraryIndex, n));
于 2013-12-30T18:51:23.903 回答
0

@Bergi:很好,但是当模数为0时它不起作用: slice(-0) 是整个数组。要修复,请使用

layers.slice(layers.length - layers.length%6).forEach(...
于 2013-08-07T05:32:29.867 回答