# __Array__ operations

[![+Professional Support](https://www.totaljs.com/img/badge-support.svg)](https://www.totaljs.com/support/) [![+Chat with contributors](https://www.totaljs.com/img/badge-chat.svg)](https://messenger.totaljs.com)

Here is the list of all registered `Array` prototypes in jComponent library:

- [`Array.async([context], [callback])`](#method-array-async-)
- [`Array.findAll([prop], value)`](#method-array-findall-)
- [`Array.findAll(fn)`](#method-array-findall-)
- [`Array.findIndex([prop], value)`](#method-array-findindex-)
- [`Array.findIndex(fn)`](#method-array-findindex-)
- [`Array.findItem([prop], value)`](#method-array-finditem-)
- [`Array.findItem(fn)`](#method-array-finditem-)
- [`Array.findValue([prop], value)`](#method-array-findvalue-)
- [`Array.quicksort(prop, [asc])`](#method-array-quicksort-)
- [`Array.remove([prop], value)`](#method-array-remove-)
- [`Array.remove(fn)`](#method-array-remove-)
- [`Array.skip(count)`](#method-array-skip-)
- [`Array.take(count)`](#method-array-take-)
- [`Array.takeskip(take, skip)`](#method-array-takeskip-)
- [`Array.ticks(max, [beg])`](#method-array-ticks-)
- [`Array.trim()`](#method-array-trim-)
- [`Array.wait(fn, [callback], [thread])`](#method-array-wait-)
---

### Method: `Array.async()`

__`REMOVED in v18`__ Method performs async operations and it expects `Array` of functions.

```javascript
Array.async([context], [callback]);
// @context {Object} optional, a function context
// @callback {Function} optional
// return {Array}

var arr = [];

arr.push(function(next) {
	console.log(1);
	setTimeout(next, 1000);
});

arr.push(function(next) {
	console.log(2);
	setTimeout(next, 1000);
});

arr.push(function(next) {
	console.log(3);
	setTimeout(next, 1000);
});

arr.async(function() {
	console.log('DONE');
});
```

### Method: `Array.findAll()`

Method finds all items according to the filter.

```javascript
Array.findAll([prop], value);
// @prop {String} optional, a property name
// @value {Object} a value to compare
// return {Array}

var arr = [];
arr.push({ id: '1', name: 'Peter', age: 30 });
arr.push({ id: '2', name: 'Anna', age: 12 });
arr.push({ id: '3', name: 'Peter', age: 33 });

console.log(arr.findAll('name', 'Peter'));

Array.findAll(fn);
// @fn {Function(item, index} a comparer
// return {Array}

var arr = [];
arr.push({ id: '1', name: 'Peter', age: 30 });
arr.push({ id: '2', name: 'Anna', age: 12 });
arr.push({ id: '3', name: 'Peter', age: 33 });

console.log(arr.findAll(function(item) {
	return item.age > 20;
}));
```

### Method: `Array.findIndex()`

Method finds an item `index` according to the filter.

```javascript
Array.findIndex([prop], value);
// @prop {String} optional, a property name
// @value {Object} a value to compare
// return {Number}

var arr = [];
arr.push({ id: '1', name: 'Peter', age: 30 });
arr.push({ id: '2', name: 'Anna', age: 12 });
arr.push({ id: '3', name: 'Peter', age: 33 });

console.log(arr.findIndex('name', 'Peter'));
// Output: 0

Array.findIndex(fn);
// @fn {Function(item, index} a comparer
// return {Number}

var arr = [];
arr.push({ id: '1', name: 'Peter', age: 30 });
arr.push({ id: '2', name: 'Anna', age: 12 });
arr.push({ id: '3', name: 'Peter', age: 33 });

console.log(arr.findIndex(function(item) {
	return item.age === 12;
}));
// Output: 1
```

### Method: `Array.findItem()`

Method finds an item according to the filter.

```javascript
Array.findItem([prop], value);
// @prop {String} optional, a property name
// @value {Object} a value to compare
// return {Object}

var arr = [];
arr.push({ id: '1', name: 'Peter', age: 30 });
arr.push({ id: '2', name: 'Anna', age: 12 });
arr.push({ id: '3', name: 'Peter', age: 33 });

console.log(arr.findItem('name', 'Peter'));
// Output: { id: '1', name: 'Peter', age: 30 }

Array.findItem(fn);
// @fn {Function(item, index} a comparer
// return {Object}

var arr = [];
arr.push({ id: '1', name: 'Peter', age: 30 });
arr.push({ id: '2', name: 'Anna', age: 12 });
arr.push({ id: '3', name: 'Peter', age: 33 });

console.log(arr.findItem(function(item) {
	return item.age === 12;
}));
// Output: { id: '2', name: 'Anna', age: 12 }
```

### Method: `Array.quicksort()`

Method can sort `Array Object` according to the property name in the first level.

```javascript
Array.quicksort(prop, [asc]);
// @prop {String} A property name
// @asc {Boolean} Optional, true = asc, false = desc (default: true)
// return {Array}
```

### Method: `Array.findValue()`

`+v16` Method finds a value according to the filter.

```javascript
Array.findValue(property, value, path, [def], [cache]);
// @prop {String} optional, a property name
// @value {Object} a value to compare
// @path {String} a path for reading value
// @def {Object} optional, a default value (default: undefined)
// @cache {Boolean} optional, value will be cached (default: false)
// return {Object}

var arr = [];
arr.push({ id: '1', name: 'Peter', age: 30 });
arr.push({ id: '2', name: 'Anna', age: 12 });
arr.push({ id: '3', name: 'Peter', age: 33 });

console.log(arr.findValue('name', 'Peter', 'age', 18));
// Output: 30
```

### Method: `Array.quicksort()`

Method can sort `Array Object` according to the property name in the first level.

```javascript
Array.quicksort(prop, [asc]);
// @prop {String} A property name
// @asc {Boolean} Optional, true = asc, false = desc (default: true)
// return {Array}
```

### Method: `Array.remove()`

Method removes all items from `Array` according to the filter.

```javascript
Array.remove([prop], value);
// @prop {String} optional, a property name
// @value {Object} a value to compare
// return {Array}

var arr = [];
arr.push({ id: '1', name: 'Peter', age: 30 });
arr.push({ id: '2', name: 'Anna', age: 12 });
arr.push({ id: '3', name: 'Peter', age: 33 });

console.log(arr.remove('name', 'Peter'));
// Output: [{ id: '2', name: 'Anna', age: 12 }]

Array.remove(fn);
// @fn {Function(item, index} a comparer
// return {Object}

var arr = [];
arr.push({ id: '1', name: 'Peter', age: 30 });
arr.push({ id: '2', name: 'Anna', age: 12 });
arr.push({ id: '3', name: 'Peter', age: 33 });

console.log(arr.remove(function(item) {
	return item.age === 12;
}));
// Output: [{ id: '1', name: 'Peter', age: 30 }, { id: '3', name: 'Peter', age: 33 }]
```

### Method: `Array.skip()`

Method skips `count` of items from `Array`.

```javascript
Array.skip(count);
// @count {Number}
// return {Array}

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
console.log(arr.skip(5));
// Output: [6, 7, 8, 9, 0]
```

### Method: `Array.take()`

Method takes `count` of items from `Array`.

```javascript
Array.take(count);
// @count {Number}
// return {Array}

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
console.log(arr.take(5));
// Output: [1, 2, 3, 4, 5]
```

### Method: `Array.takeskip()`

Method takes `take` and skips `skip` of items from `Array`.

```javascript
Array.takeskip(take, skip);
// @take {Number}
// @skip {Number}
// return {Array}

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
console.log(arr.takeskip(2, 3));
// Output: [4, 5]
```

### Method: `Array.ticks()`

Method can resize a large array to required size.

```javascript
Array.ticks(max, [beg]);
// @take {Number}
// @beg {Boolean} optional, first number must be first item from Array (default: false)
// return {Array}

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];

console.log(arr.ticks(3));
// Output: [4, 7, 0]

console.log(arr.ticks(3, true));
// Output: [4, 7, 0]
```

### Method: `Array.trim()`

Method removes empty/nullable items from `Array`.

```javascript
Array.trim();
// return {Array}

var arr = [1, 2, null, 4, 5, '', 7, 8, 9, 0];

console.log(arr.trim());
// Output: [1, 2, 4, 5, 7, 8, 9]
```

### Method: `Array.wait()`

Method processes each item in `Array` asynchronously.

```javascript
Array.wait(fn, callback);
// @fn {Function(item, next, index)}
// @callback {Funciton} optional
// @thread {Number} optional, thread count (default: 1)
// return {Array}

var arr = [1, 2, 3, 4];

arr.wait(function(item, next, index) {
	console.log(item);
	setTimeout(next, 1000);
}, function() {
	console.log('DONE');
});

// Output:
// 1
// 2
// 3
// 4
// DONE
```