jComponent / Helpers / Array operations
Updated: 17. August 2019
Author: Peter Širka

Array operations

Professional Support Chat with contributors

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

Method: Array.async()

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

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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