Spanish Total.js
English
Documentation For AI agents
Contents

Array

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

.md GitHub
On this page

jComponent adds these methods to every array. Lists prepared with them are often rendered with Templates with Tangular.

API

method

Array.attr(name, [value])

Adds an HTML attribute as text to the array.
ParameterTypeDescription
name String The attribute name.
value? Object The attribute value.

Returns Array The same array.

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

Building attributes
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"
method

Array.findAll(filter, [value])

Returns all the items that match.
ParameterTypeDescription
filter String, Function or Object The field name, the test function or the value to find.
value? Object 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.

Finding items
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]
method

Array.findIndex(filter, [value])

Returns the index of the first item that matches.
ParameterTypeDescription
filter String, Function or Object The field name, the test function or the value to find.
value? Object The value of the field.

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

The arguments work as in findAll().

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

Array.findItem(filter, [value])

Returns the first item that matches.
ParameterTypeDescription
filter String, Function or Object The field name, the test function or the value to find.
value? Object The value of the field.

Returns Object The item, or undefined when nothing matches.

The arguments work as in findAll().

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

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

Returns one field of the first item that matches.
ParameterTypeDescription
filter String or Function The field to compare, or the test function.
value Object The value of the compared field.
field String The name of the field to return.
def? Object The value returned when no item matches or the field is empty.
cache? Boolean Keeps the result for later calls. Default: false

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 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.

Reading a field
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
method

Array.group(key)

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

Grouping
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}]}]
method

Array.last([def])

Returns the last item.
ParameterTypeDescription
def? Object The value returned for an empty array.

Returns Object The last item or def.

Last item
console.log([1, 2, 3].last()); // 3
console.log([].last('empty')); // empty
method

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

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

Plain values
console.log(['b', 'c', 'a'].quicksort()); // ["a","b","c"]
Objects
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
method

Array.remove(filter, [value])

Returns a copy of the array without the items that match.
ParameterTypeDescription
filter String, Function or Object The field name, the test function or the value to remove.
value? Object 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.

Removing items
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]
method

Array.skip(count)

Returns the items after the first count items.
ParameterTypeDescription
count Number The number of items to skip.

Returns Array A new array.

Skipping
console.log([1, 2, 3, 4].skip(2)); // [3,4]
method

Array.take(count)

Returns the first count items.
ParameterTypeDescription
count Number The number of items to return, from 1 up.

Returns Array A new array.

Taking
console.log([1, 2, 3].take(2)); // [1,2]
console.log([1].take(5)); // [1]
method

Array.takeskip(take, skip)

Skips a number of items and returns the following ones.
ParameterTypeDescription
take Number The number of items to return.
skip Number The number of items to skip.

Returns Array A new array.

The method is useful for paging.

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

Array.trim()

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.

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

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

Processes the items one after another, or several at a time.
ParameterTypeDescription
onItem Function The function that processes an item.
callback? Function The function called at the end.
thread? Number or Boolean The number of parallel items, or true to empty the array. Default: 1

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.

One item at a time
var log = [];
[10, 20].wait(function(item, next, index) {
	log.push(index + ':' + item);
	setTimeout(next, 10);
}, function() {
	console.log(log); // ["1:10","2:20"]
});
Two items at a time
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);
Emptying the array
var queue = [1, 2, 3];
queue.wait((item, next) => next(), function() {
	console.log(queue.length); // 0
}, true);
Made in Europe