|
Last change
on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
802 bytes
|
| Line | |
|---|
| 1 | /**
|
|---|
| 2 | * Given an array and a number N, return a new array which contains every nTh
|
|---|
| 3 | * element of the input array. For n below 1, an empty array is returned.
|
|---|
| 4 | * For n equal to 1, the input array is returned as is.
|
|---|
| 5 | * For n greater than the length of the array, an array containing the first element
|
|---|
| 6 | * and every nTh element after that (if any) is returned.
|
|---|
| 7 | *
|
|---|
| 8 | * @param array An input array.
|
|---|
| 9 | * @param n A number specifying which elements to take.
|
|---|
| 10 | * @returns The result array of the same type as the input array.
|
|---|
| 11 | */
|
|---|
| 12 | export function getEveryNth(array, n) {
|
|---|
| 13 | if (n < 1) {
|
|---|
| 14 | return [];
|
|---|
| 15 | }
|
|---|
| 16 | if (n === 1) {
|
|---|
| 17 | return array;
|
|---|
| 18 | }
|
|---|
| 19 | var result = [];
|
|---|
| 20 | for (var i = 0; i < array.length; i += n) {
|
|---|
| 21 | var item = array[i];
|
|---|
| 22 | if (item !== undefined) {
|
|---|
| 23 | result.push(item);
|
|---|
| 24 | }
|
|---|
| 25 | }
|
|---|
| 26 | return result;
|
|---|
| 27 | } |
|---|
Note:
See
TracBrowser
for help on using the repository browser.