---
title: Array
project: jComponent V19
version: 19.194
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/jcomponent-v19/19-194/reference/array/
updated: 2026-11-18T13:58:29.465Z
---

# Array

> Methods that jComponent adds to Array.prototype for finding, sorting, grouping and processing items.

jComponent adds these methods to `Array.prototype`. `findIndex()` replaces the native method and accepts the arguments described here. The find methods are often used with code lists stored in [DEF](../def/).

## API

### arr.async([threads], [callback])

Type: `method`

Runs an array of functions one after another or in parallel.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `threads` | `Number` | no | `1` | How many functions run at the same time. |
| `callback` | `Function` | no |  | Called when all functions have finished. |

Returns: `Array` The same array.

Each function receives a `next` function that it must call when its work is done. The array is emptied while it runs. `threads` must not be higher than the number of functions in the array.

Example: Run tasks in order
```js
var log = [];

var tasks = [
	function(next) { log.push('first'); setTimeout(next, 20); },
	function(next) { log.push('second'); next(); }
];

tasks.async(function() {
	console.log(log.join(', '));
});
```

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

Type: `method`

Adds an HTML attribute to an array used as an HTML builder.

| 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 HTML-encoded and goes through `env()`. With one argument, the attribute name is also its value. With two arguments, a `null` or `undefined` value adds nothing.

Example: Build an element
```js
var builder = ['<div'];
builder.attr('id', 'box').attr('title', 'a<b').attr('hidden').attr('data-x', null);
builder.join(' ') + '>'; // "<div id=\"box\" title=\"a&lt;b\" hidden=\"hidden\">"
```

### arr.findAll(cb, [value])

Type: `method`

Returns all items that match a condition.

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

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

The condition is one of three forms: a `function(item, index)`, a field name with a value, or a value compared with each item.

Example: Find many items
```js
var users = [
	{ id: 1, name: 'Ann', age: 30 },
	{ id: 2, name: 'Bob', age: 25 },
	{ id: 3, name: 'Cid', age: 35 }
];

users.findAll('age', 25).length; // 1
users.findAll(user => user.age >= 30).length; // 2
['a', 'b', 'a'].findAll('a'); // ["a","a"]
```

### arr.findIndex(cb, [value])

Type: `method`

Returns the index of the first item that matches a condition.

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

Returns: `Number` The index, or `-1`.

The condition has the same forms as in [findAll()](#findall).

Example: Find an index
```js
var users = [
	{ id: 1, name: 'Ann', age: 30 },
	{ id: 2, name: 'Bob', age: 25 },
	{ id: 3, name: 'Cid', age: 35 }
];

users.findIndex('id', 2); // 1
users.findIndex(user => user.age > 30); // 2
users.findIndex('id', 99); // -1
['a', 'b'].findIndex('b'); // 1
```

### arr.findItem(cb, [value])

Type: `method`

Returns the first item that matches a condition.

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

Returns: `Object` The item, or `undefined`.

The condition has the same forms as in [findAll()](#findall).

Example: Find an item
```js
var users = [
	{ id: 1, name: 'Ann', age: 30 },
	{ id: 2, name: 'Bob', age: 25 },
	{ id: 3, name: 'Cid', age: 35 }
];

users.findItem('id', 2).name; // "Bob"
users.findItem(user => user.age > 30).name; // "Cid"
users.findItem('id', 99); // undefined
```

### arr.findValue(cb, value, path, [def])

Type: `method`

Finds an item and returns one of its values.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cb` | `String or Function` | yes |  | A field name or a `function(item, index)`. |
| `value` | `Object` | yes |  | The value of the field. |
| `path` | `String` | yes |  | The path of the value to read from the item. |
| `def` | `Object` | no |  | The value returned when the item or its value does not exist. |

Returns: `Object` The value that was read, or `def`.

With a function as the condition, leave out `value`: `arr.findValue(fn, path, [def])`. The `path` can be nested, such as `address.city`.

Example: Read a value
```js
var users = [
	{ id: 1, name: 'Ann', address: { city: 'Rome' } },
	{ id: 2, name: 'Bob' }
];

users.findValue('id', 1, 'name'); // "Ann"
users.findValue('id', 1, 'address.city'); // "Rome"
users.findValue('id', 9, 'name', 'unknown'); // "unknown"
users.findValue(user => user.id === 2, 'name'); // "Bob"
```

### arr.group(name)

Type: `method`

Groups objects by the value of a field.

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

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

The group names are texts, and the groups are sorted by name as text. Items without a value go to a group with an empty name.

Example: Group users
```js
var users = [{ name: 'Ann', role: 'dev' }, { name: 'Bob', role: 'ops' }, { name: 'Cid', role: 'dev' }, { name: 'Dan' }];
users.group('role').map(group => group.name + ':' + group.items.length); // [":1","dev:2","ops:1"]
```

### arr.last([def])

Type: `method`

Returns the last item of the array.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `def` | `Object` | no |  | The value returned when the last item is `undefined`. |

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

Example: Read the last item
```js
[1, 2, 3].last(); // 3
[].last('empty'); // "empty"
```

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

Type: `method`

Sorts the array in place by one or more fields.

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

Returns: `Array` The same array.

`sort` contains field names separated by commas. Each name can end with `_asc` or `_desc`, and it can be a nested path such as `address.city`. Text uses a locale-aware comparison. Numbers, booleans and dates are compared by their type.

Without `sort`, the items themselves are compared as text in ascending order, so numbers sort as text.

Example: Sort by fields
```js
var users = [{ name: 'Bob', age: 25 }, { name: 'Ann', age: 30 }, { name: 'Cid', age: 25 }];
var names = () => users.map(user => user.name).join();

users.quicksort('name');
names(); // "Ann,Bob,Cid"
users.quicksort('name_desc');
names(); // "Cid,Bob,Ann"
users.quicksort('name', 'desc');
names(); // "Cid,Bob,Ann"
users.quicksort('age,name_desc');
names(); // "Cid,Bob,Ann"
```

Example: Sort values
```js
['b', 'c', 'a'].quicksort(); // ["a","b","c"]
[10, 9, 100].quicksort(); // [10,100,9]
```

### arr.remove(cb, [value])

Type: `method`

Returns a new array without the items that match a condition.

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

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

The original array is not changed. The condition has the same forms as in [findAll()](#findall).

Example: Remove items
```js
var users = [
	{ id: 1, name: 'Ann', age: 30 },
	{ id: 2, name: 'Bob', age: 25 },
	{ id: 3, name: 'Cid', age: 35 }
];

users.remove('id', 2).length; // 2
users.remove(user => user.age > 26).length; // 1
['a', 'b', 'a'].remove('a'); // ["b"]
users.length; // 3
```

### arr.skip(count)

Type: `method`

Returns the items after the first `count` items.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `count` | `Number` | yes |  | How many items to skip. |

Returns: `Array` A new array.

Example: Skip items
```js
[1, 2, 3, 4].skip(2); // [3,4]
```

### arr.take(count)

Type: `method`

Returns the first `count` items.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `count` | `Number` | yes |  | How many items to take. |

Returns: `Array` A new array.

The count must be 1 or more.

Example: Take items
```js
[1, 2, 3, 4].take(2); // [1,2]
[1, 2].take(9); // [1,2]
```

### arr.takeskip(take, skip)

Type: `method`

Skips a number of items and returns the next ones.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `take` | `Number` | yes |  | How many items to return. |
| `skip` | `Number` | yes |  | How many items to skip. |

Returns: `Array` A new array.

This is useful for reading one page of a list.

Example: Read a page
```js
[1, 2, 3, 4, 5].takeskip(2, 1); // [2,3]
```

### arr.trim([empty])

Type: `method`

Trims the strings of the array and removes empty values.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `empty` | `Boolean` | no |  | Use `true` to keep empty values. |

Returns: `Array` A new array.

Strings are trimmed in the original array too. Without `empty`, the result leaves out every value that is falsy, including `0` and `null`.

Example: Clean a list
```js
[' a ', '', ' b', 0, 1].trim(); // ["a","b",1]
[' a ', '', 0].trim(true); // ["a","",0]
```

### arr.wait(onItem, [callback], [threads])

Type: `method`

Processes the items one by one or in parallel with an asynchronous function.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `onItem` | `Function` | yes |  | Processes one item. |
| `callback` | `Function` | no |  | Called when all items are done. |
| `threads` | `Number or Boolean` | no | `1` | How many items run at the same time, or `true` to remove processed items. |

Returns: `Array` The same array.

`onItem` is called as `function(item, next, index)` with the array as `this`, and it must call `next` when the item is done. `index` is the position of the item, starting at 1.

When `threads` is `true`, the items are removed from the array as they are processed, and `index` is always `0`.

Example: Process items in order
```js
var output = [];

['a', 'b', 'c'].wait(function(item, next, index) {
	output.push(index + ':' + item);
	setTimeout(next, 10);
}, function() {
	console.log(output.join(', '));
});
```

Example: Two items at a time
```js
var output = [];

[1, 2, 3].wait(function(item, next) {
	output.push('start ' + item);
	setTimeout(function() {
		output.push('end ' + item);
		next();
	}, item === 1 ? 30 : 5);
}, function() {
	console.log(output.join(', '));
}, 2);
```

Example: Remove processed items
```js
var items = [1, 2];

items.wait(function(item, next) {
	next();
}, function() {
	console.log(items.length);
}, true);
```
