---
title: Array
project: jComponent V20
version: 20.015
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/jcomponent-v20/20-015/reference/array/
updated: 2026-11-18T13:58:19.462Z
---

# Array

> Methods that jComponent adds to Array.prototype for searching, sorting, slicing and processing lists.

jComponent adds these methods to every array. Lists prepared with them are often rendered with [Templates with Tangular](../../guides/tangular/).

## API

### Array.attr(name, [value])

Type: `method`

Adds an HTML attribute as text to the array.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The attribute name. |
| `value` | `Object` | no |  | The attribute value. |

Returns: `Array` The same array.

The value is escaped and `[key]` phrases in it are replaced as in [env()](../string/). Without `value` the attribute name is also its value. A `null` value adds nothing.

Example: Building attributes
```js
var attrs = [];
attrs.attr('id', 'box').attr('title', '<b>').attr('disabled').attr('hidden', null);
console.log(attrs.join(' ')); // id="box" title="&lt;b&gt;" disabled="disabled"
```

### Array.findAll(filter, [value])

Type: `method`

Returns all the items that match.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `filter` | `String, Function or Object` | yes |  | The field name, the test function or the value to find. |
| `value` | `Object` | no |  | The value of the field. |

Returns: `Array` A new array with the matching items.

With `filter` and `value`, the method compares the field named `filter` with `value`. With a function, it receives `(item, index)`. With one other value, it compares the items themselves.

Example: Finding items
```js
console.log([{ a: 1 }, { a: 2 }, { a: 1 }].findAll('a', 1)); // [{"a":1},{"a":1}]
console.log([1, 2, 3].findAll(value => value > 1)); // [2,3]
console.log([1, 2, 1].findAll(1)); // [1,1]
```

### Array.findIndex(filter, [value])

Type: `method`

Returns the index of the first item that matches.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `filter` | `String, Function or Object` | yes |  | The field name, the test function or the value to find. |
| `value` | `Object` | no |  | The value of the field. |

Returns: `Number` The index, or `-1` when nothing matches.

The arguments work as in `findAll()`.

Example: Finding an index
```js
console.log([{ a: 1 }, { a: 2 }].findIndex('a', 2)); // 1
console.log([5, 6].findIndex((value, index) => index === 1)); // 1
console.log([1, 2].findIndex(9)); // -1
```

### Array.findItem(filter, [value])

Type: `method`

Returns the first item that matches.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `filter` | `String, Function or Object` | yes |  | The field name, the test function or the value to find. |
| `value` | `Object` | no |  | The value of the field. |

Returns: `Object` The item, or `undefined` when nothing matches.

The arguments work as in `findAll()`.

Example: Finding an item
```js
console.log([{ id: 1, name: 'A' }].findItem('id', 1)); // {"id":1,"name":"A"}
console.log([1, 2, 3].findItem(value => value > 1)); // 2
console.log([1].findItem(9) === undefined); // true
```

### Array.findValue(filter, value, field, [def], [cache])

Type: `method`

Returns one field of the first item that matches.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `filter` | `String or Function` | yes |  | The field to compare, or the test function. |
| `value` | `Object` | yes |  | The value of the compared field. |
| `field` | `String` | yes |  | The name of the field to return. |
| `def` | `Object` | no |  | The value returned when no item matches or the field is empty. |
| `cache` | `Boolean` | no | `false` | Keeps the result for later calls. |

Returns: `Object` The value of the field.

With a function as the filter, the signature is `findValue(fn, field, [def], [cache])`. A nested `field` is written with dots, for example `user.age`.

With `cache` set to `true`, the result is kept in [TEMP](../globals/) under a key made of the filter, the value and the field. A later call with the same arguments returns the kept result, even for another array.

Example: Reading a field
```js
var users = [{ id: 1, name: 'Ana' }];
console.log(users.findValue('id', 1, 'name')); // Ana
console.log(users.findValue('id', 9, 'name', 'nobody')); // nobody
console.log(users.findValue(user => user.id === 1, 'name', 'nobody')); // Ana
console.log([{ id: 1, user: { age: 5 } }].findValue('id', 1, 'user.age')); // 5
```

### Array.group(key)

Type: `method`

Groups the items by the value of a field.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `key` | `String` | yes |  | The field name. |

Returns: `Array` Objects with the fields `name` and `items`.

The groups are sorted by name. Items without the field are in a group with an empty name.

Example: Grouping
```js
var list = [{ type: 'b', id: 1 }, { type: 'a', id: 2 }, { type: 'b', id: 3 }];
console.log(list.group('type')); // [{"name":"a","items":[{"type":"a","id":2}]},{"name":"b","items":[{"type":"b","id":1},{"type":"b","id":3}]}]
```

### Array.last([def])

Type: `method`

Returns the last item.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `def` | `Object` | no |  | The value returned for an empty array. |

Returns: `Object` The last item or `def`.

Example: Last item
```js
console.log([1, 2, 3].last()); // 3
console.log([].last('empty')); // empty
```

### Array.quicksort([sort], [direction])

Type: `method`

Sorts the array in place.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `sort` | `String` | no |  | The fields to sort by. |
| `direction` | `Boolean, String or Number` | no |  | `false`, `desc` or `2` sorts one field in descending order. |

Returns: `Array` The same array.

Without arguments, the items are sorted in ascending order and compared as text.

A string sorts objects by their fields. Each field can end with `_asc` or `_desc`, several fields are separated by commas, and a nested field is written with dots. Strings, numbers, booleans and dates are supported.

Example: Plain values
```js
console.log(['b', 'c', 'a'].quicksort()); // ["a","b","c"]
```

Example: Objects
```js
var list = [{ name: 'b', age: 1 }, { name: 'a', age: 2 }, { name: 'a', age: 3 }];
console.log(list.quicksort('name_asc,age_desc')); // [{"name":"a","age":3},{"name":"a","age":2},{"name":"b","age":1}]
console.log(list.quicksort('age', 'desc')[0].age); // 3
console.log([{ user: { age: 2 } }, { user: { age: 1 } }].quicksort('user.age')[0].user.age); // 1
```

### Array.remove(filter, [value])

Type: `method`

Returns a copy of the array without the items that match.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `filter` | `String, Function or Object` | yes |  | The field name, the test function or the value to remove. |
| `value` | `Object` | no |  | The value of the field. |

Returns: `Array` A new array with the remaining items.

The arguments work as in `findAll()`. The original array does not change.

Example: Removing items
```js
console.log([{ a: 1 }, { a: 2 }].remove('a', 1)); // [{"a":2}]
console.log([1, 2, 3].remove(value => value > 1)); // [1]
console.log([1, 2, 1].remove(1)); // [2]
```

### Array.skip(count)

Type: `method`

Returns the items after the first `count` items.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `count` | `Number` | yes |  | The number of items to skip. |

Returns: `Array` A new array.

Example: Skipping
```js
console.log([1, 2, 3, 4].skip(2)); // [3,4]
```

### Array.take(count)

Type: `method`

Returns the first `count` items.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `count` | `Number` | yes |  | The number of items to return, from 1 up. |

Returns: `Array` A new array.

Example: Taking
```js
console.log([1, 2, 3].take(2)); // [1,2]
console.log([1].take(5)); // [1]
```

### Array.takeskip(take, skip)

Type: `method`

Skips a number of items and returns the following ones.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `take` | `Number` | yes |  | The number of items to return. |
| `skip` | `Number` | yes |  | The number of items to skip. |

Returns: `Array` A new array.

The method is useful for paging.

Example: A page of items
```js
console.log([1, 2, 3, 4, 5].takeskip(2, 1)); // [2,3]
```

### Array.trim()

Type: `method`

Trims the strings and leaves out the empty values.

Returns: `Array` A new array with the remaining values.

Every falsy value is left out, including `0`. The strings in the original array are trimmed too.

Example: Cleaning a list
```js
console.log([' a ', '', 0, null, 'b ', 5].trim()); // ["a","b",5]
```

### Array.wait(onItem, [callback], [thread])

Type: `method`

Processes the items one after another, or several at a time.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `onItem` | `Function` | yes |  | The function that processes an item. |
| `callback` | `Function` | no |  | The function called at the end. |
| `thread` | `Number or Boolean` | no | `1` | The number of parallel items, or `true` to empty the array. |

Returns: `Array` The same array.

`onItem` receives `(item, next, index)` and must call `next()` when the item is finished. `index` is the position of the item, starting at 1. `callback` runs when all the items are finished, and it can be left out with `thread` passed in its place.

A number in `thread` sets how many items run at the same time. `true` removes each item from the array as it is processed, and `index` is then `0`.

Example: One item at a time
```js
var log = [];
[10, 20].wait(function(item, next, index) {
	log.push(index + ':' + item);
	setTimeout(next, 10);
}, function() {
	console.log(log); // ["1:10","2:20"]
});
```

Example: Two items at a time
```js
var running = 0;
var max = 0;
[1, 2, 3, 4, 5].wait(function(item, next) {
	running++;
	max = Math.max(max, running);
	setTimeout(function() {
		running--;
		next();
	}, 10);
}, function() {
	console.log(max); // 2
}, 2);
```

Example: Emptying the array
```js
var queue = [1, 2, 3];
queue.wait((item, next) => next(), function() {
	console.log(queue.length); // 0
}, true);
```
