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

# Array

> Metodi che jComponent aggiunge a Array.prototype per trovare, ordinare, raggruppare ed elaborare gli elementi.

jComponent aggiunge questi metodi a `Array.prototype`. `findIndex()` sostituisce il metodo nativo e accetta gli argomenti descritti qui. I metodi di ricerca si usano spesso con gli elenchi di codici memorizzati in [DEF](../def/).

## API

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

Type: `method`

Esegue un array di funzioni una dopo l'altra o in parallelo.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `threads` | `Number` | no | `1` | Quante funzioni vengono eseguite contemporaneamente. |
| `callback` | `Function` | no |  | Chiamato quando tutte le funzioni hanno terminato. |

Returns: `Array` Lo stesso array.

Ogni funzione riceve una funzione `next` che deve chiamare quando ha finito il proprio lavoro. L'array viene svuotato durante l'esecuzione. `threads` non deve superare il numero di funzioni nell'array.

Example: Esegui le attività in ordine
```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`

Aggiunge un attributo HTML a un array usato per costruire HTML.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Il nome dell'attributo. |
| `value` | `Object` | no |  | Il valore dell'attributo. |

Returns: `Array` Lo stesso array.

Il valore viene codificato in HTML e passa attraverso `env()`. Con un solo argomento, il nome dell'attributo è anche il suo valore. Con due argomenti, un valore `null` o `undefined` non aggiunge nulla.

Example: Costruisci un elemento
```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`

Restituisce tutti gli elementi che soddisfano una condizione.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cb` | `String, Function or Object` | yes |  | Una funzione, un nome di campo o un valore da trovare. |
| `value` | `Object` | no |  | Il valore del campo. |

Returns: `Array` Un nuovo array con gli elementi corrispondenti.

La condizione ha una di tre forme: una `function(item, index)`, un nome di campo con un valore, oppure un valore confrontato con ogni elemento.

Example: Trova più elementi
```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`

Restituisce l'indice del primo elemento che soddisfa una condizione.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cb` | `String, Function or Object` | yes |  | Una funzione, un nome di campo o un valore da trovare. |
| `value` | `Object` | no |  | Il valore del campo. |

Returns: `Number` L'indice, oppure `-1`.

La condizione ha le stesse forme di [findAll()](#findall).

Example: Trova un indice
```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`

Restituisce il primo elemento che soddisfa una condizione.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cb` | `String, Function or Object` | yes |  | Una funzione, un nome di campo o un valore da trovare. |
| `value` | `Object` | no |  | Il valore del campo. |

Returns: `Object` L'elemento, oppure `undefined`.

La condizione ha le stesse forme di [findAll()](#findall).

Example: Trova un elemento
```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`

Trova un elemento e restituisce uno dei suoi valori.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cb` | `String or Function` | yes |  | Un nome di campo o una `function(item, index)`. |
| `value` | `Object` | yes |  | Il valore del campo. |
| `path` | `String` | yes |  | Il path del valore da leggere dall'elemento. |
| `def` | `Object` | no |  | Il valore restituito quando l'elemento o il suo valore non esiste. |

Returns: `Object` Il valore letto, oppure `def`.

Con una funzione come condizione, ometti `value`: `arr.findValue(fn, path, [def])`. Il `path` può essere annidato, come `address.city`.

Example: Leggi un valore
```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`

Raggruppa gli oggetti in base al valore di un campo.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Il nome del campo. |

Returns: `Array` Oggetti con i campi `name` e `items`.

I nomi dei gruppi sono testi e i gruppi vengono ordinati per nome come testo. Gli elementi senza valore finiscono in un gruppo con nome vuoto.

Example: Raggruppa gli utenti
```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`

Restituisce l'ultimo elemento dell'array.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `def` | `Object` | no |  | Il valore restituito quando l'ultimo elemento è `undefined`. |

Returns: `Object` L'ultimo elemento, oppure `def`.

Example: Leggi l'ultimo elemento
```js
[1, 2, 3].last(); // 3
[].last('empty'); // "empty"
```

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

Type: `method`

Ordina l'array sul posto in base a uno o più campi.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `sort` | `String` | no |  | I campi in base ai quali ordinare. |
| `direction` | `String, Boolean or Number` | no |  | Usa `'desc'`, `false` o `2` per ordinare un singolo campo in ordine decrescente. |

Returns: `Array` Lo stesso array.

`sort` contiene nomi di campo separati da virgole. Ogni nome può terminare con `_asc` o `_desc` e può essere un path annidato come `address.city`. Il testo usa un confronto basato sulla lingua. Numeri, booleani e date vengono confrontati in base al tipo.

Senza `sort`, gli elementi stessi vengono confrontati come testo in ordine crescente, quindi anche i numeri vengono ordinati come testo.

Example: Ordina per campi
```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: Ordina i valori
```js
['b', 'c', 'a'].quicksort(); // ["a","b","c"]
[10, 9, 100].quicksort(); // [10,100,9]
```

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

Type: `method`

Restituisce un nuovo array senza gli elementi che soddisfano una condizione.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cb` | `String, Function or Object` | yes |  | Una funzione, un nome di campo o un valore da rimuovere. |
| `value` | `Object` | no |  | Il valore del campo. |

Returns: `Array` Un nuovo array con gli elementi rimanenti.

L'array originale non viene modificato. La condizione ha le stesse forme di [findAll()](#findall).

Example: Rimuovi gli elementi
```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`

Restituisce gli elementi che seguono i primi `count` elementi.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `count` | `Number` | yes |  | Quanti elementi saltare. |

Returns: `Array` Un nuovo array.

Example: Salta gli elementi
```js
[1, 2, 3, 4].skip(2); // [3,4]
```

### arr.take(count)

Type: `method`

Restituisce i primi `count` elementi.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `count` | `Number` | yes |  | Quanti elementi prendere. |

Returns: `Array` Un nuovo array.

Il conteggio deve essere pari o superiore a 1.

Example: Prendi gli elementi
```js
[1, 2, 3, 4].take(2); // [1,2]
[1, 2].take(9); // [1,2]
```

### arr.takeskip(take, skip)

Type: `method`

Salta un certo numero di elementi e restituisce i successivi.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `take` | `Number` | yes |  | Quanti elementi restituire. |
| `skip` | `Number` | yes |  | Quanti elementi saltare. |

Returns: `Array` Un nuovo array.

È utile per leggere una pagina di un elenco.

Example: Leggi una pagina
```js
[1, 2, 3, 4, 5].takeskip(2, 1); // [2,3]
```

### arr.trim([empty])

Type: `method`

Elimina gli spazi dalle stringhe dell'array e rimuove i valori vuoti.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `empty` | `Boolean` | no |  | Usa `true` per mantenere i valori vuoti. |

Returns: `Array` Un nuovo array.

Gli spazi vengono eliminati anche dalle stringhe dell'array originale. Senza `empty`, il risultato esclude ogni valore falsy, compresi `0` e `null`.

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

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

Type: `method`

Elabora gli elementi uno alla volta o in parallelo con una funzione asincrona.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `onItem` | `Function` | yes |  | Elabora un elemento. |
| `callback` | `Function` | no |  | Chiamato quando tutti gli elementi sono completati. |
| `threads` | `Number or Boolean` | no | `1` | Quanti elementi vengono elaborati contemporaneamente, oppure `true` per rimuovere gli elementi elaborati. |

Returns: `Array` Lo stesso array.

`onItem` viene chiamata come `function(item, next, index)` con l'array come `this` e deve chiamare `next` quando l'elemento è completato. `index` è la posizione dell'elemento, a partire da 1.

Quando `threads` è `true`, gli elementi vengono rimossi dall'array man mano che vengono elaborati e `index` è sempre `0`.

Example: Elabora gli elementi in ordine
```js
var output = [];

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

Example: Due elementi alla volta
```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: Rimuovi gli elementi elaborati
```js
var items = [1, 2];

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