<commit_before>---
layout: post
title: Cycle an array using ES2015
tags:
- javascript
---
ES2015 is fun and allows you to write more concise Javascript.

Here's how to cycle an array of values. Use it to zebra stripe a table or whatever:

```js
function cycle (...args) {
  return (i) => args[i % args.length]
}
```

You could even write it even shorter:

```js
const cycle = (...args) => (i) => args[i % args.length]
```

Use it in React like this:

```js
function Table ({ posts }) {
  const cls = cycle('odd', 'even')<commit_msg>still shorter<commit_after>---
layout: post
title: Cycle an array using ES2015
tags:
- javascript
---
ES2015 is fun and allows you to write more concise Javascript.

Here's how to cycle an array of values. Use it to zebra stripe a table or whatever:

```js
function cycle (...args) {
  return (i) => args[i % args.length]
}
```

And we can still make it shorter:

```js
const cycle = (...args) => (i) => args[i % args.length]
```

Use it in React like this:

```js
function Table ({ posts }) {
  const cls = cycle('odd', 'even')
{'fraction_non_alphanumeric': 0.1471698113207547, 'fraction_numerical': 0.01509433962264151, 'mean_word_length': 3.4957627118644066, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '815562', 'n_tokens_mistral': 377, 'n_tokens_neox': 335, 'n_words': 150}