Spanish Total.js
English
Documentation For AI agents
Contents

Array

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

.md GitHub
On this page

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.

API

method

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

Runs an array of functions one after another or in parallel.
ParameterTypeDescription
threads? Number How many functions run at the same time. Default: 1
callback? Function 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.

Run tasks in order
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(', '));
});
method

arr.attr(name, [value])

Adds an HTML attribute to an array used as an HTML builder.
ParameterTypeDescription
name String The attribute name.
value? Object 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.

Build an element
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\">"
method

arr.findAll(cb, [value])

Returns all items that match a condition.
ParameterTypeDescription
cb String, Function or Object A function, a field name or a value to find.
value? Object 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.

Find many items
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"]
method

arr.findIndex(cb, [value])

Returns the index of the first item that matches a condition.
ParameterTypeDescription
cb String, Function or Object A function, a field name or a value to find.
value? Object The value of the field.

Returns Number The index, or -1.

The condition has the same forms as in findAll().

Find an index
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
method

arr.findItem(cb, [value])

Returns the first item that matches a condition.
ParameterTypeDescription
cb String, Function or Object A function, a field name or a value to find.
value? Object The value of the field.

Returns Object The item, or undefined.

The condition has the same forms as in findAll().

Find an item
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
method

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

Finds an item and returns one of its values.
ParameterTypeDescription
cb String or Function A field name or a function(item, index).
value Object The value of the field.
path String The path of the value to read from the item.
def? Object 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.

Read a value
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"
method

arr.group(name)

Groups objects by the value of a field.
ParameterTypeDescription
name String 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.

Group users
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"]
method

arr.last([def])

Returns the last item of the array.
ParameterTypeDescription
def? Object The value returned when the last item is undefined.

Returns Object The last item, or def.

Read the last item
[1, 2, 3].last(); // 3
[].last('empty'); // "empty"
method

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

Sorts the array in place by one or more fields.
ParameterTypeDescription
sort? String The fields to sort by.
direction? String, Boolean or Number 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.

Sort by fields
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"
Sort values
['b', 'c', 'a'].quicksort(); // ["a","b","c"]
[10, 9, 100].quicksort(); // [10,100,9]
method

arr.remove(cb, [value])

Returns a new array without the items that match a condition.
ParameterTypeDescription
cb String, Function or Object A function, a field name or a value to remove.
value? Object 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().

Remove items
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
method

arr.skip(count)

Returns the items after the first count items.
ParameterTypeDescription
count Number How many items to skip.

Returns Array A new array.

Skip items
[1, 2, 3, 4].skip(2); // [3,4]
method

arr.take(count)

Returns the first count items.
ParameterTypeDescription
count Number How many items to take.

Returns Array A new array.

The count must be 1 or more.

Take items
[1, 2, 3, 4].take(2); // [1,2]
[1, 2].take(9); // [1,2]
method

arr.takeskip(take, skip)

Skips a number of items and returns the next ones.
ParameterTypeDescription
take Number How many items to return.
skip Number How many items to skip.

Returns Array A new array.

This is useful for reading one page of a list.

Read a page
[1, 2, 3, 4, 5].takeskip(2, 1); // [2,3]
method

arr.trim([empty])

Trims the strings of the array and removes empty values.
ParameterTypeDescription
empty? Boolean 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.

Clean a list
[' a ', '', ' b', 0, 1].trim(); // ["a","b",1]
[' a ', '', 0].trim(true); // ["a","",0]
method

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

Processes the items one by one or in parallel with an asynchronous function.
ParameterTypeDescription
onItem Function Processes one item.
callback? Function Called when all items are done.
threads? Number or Boolean How many items run at the same time, or true to remove processed items. Default: 1

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.

Process items in order
var output = [];

['a', 'b', 'c'].wait(function(item, next, index) {
	output.push(index + ':' + item);
	setTimeout(next, 10);
}, function() {
	console.log(output.join(', '));
});
Two items at a time
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);
Remove processed items
var items = [1, 2];

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