Array

link Function Description
# each Loops through an array or object and runs a callback on each iteration. Returning false will break the loop.
# map Returns a new object or array by through and running a callback to accept or reject each iteration. Returning false rejects item. Returning undefined stops the loop. Any other return value will be set on the object or array under the current key/index.
# array_unique Filters out non-unique values from an array.
# array_filter Filters out empty items from an array or object and returns a new Array or Object.
Does not modify the original Array or Object, rather it will filter out empty items and return a new Object or Array.
# array_set Sets a value to array or object using dot.notation.
Sets key/value an pair to any nested array or object via dot.notation. If parent items do not exist in the path, they are created.
# array_get Returns an item from an array or object using dot.notation.
Returns the item indexed by the path or undefined if it does not exist.
# array_has Checks if an item from an array or object exists using dot.notation and returns a boolean.
# array_delete Deletes an item from an array or object exists using dot.notation.
# in_array Checks if an item exists in array or object.


each

Loops through an array or object and runs a callback on each iteration. Returning false will break the loop.

each(Array|Object: array, Function: callback, ?Mixed: this, ...args): Void
// Loop an array
each(['one','two','three'], (index, value) => console.log(index, value));

// Loop an object
each({foo: 'one', bar: 'two'}, (key, value) => console.log(key, value));

// Stop loop
each(['one','two','three'], (index, value) => index >= 1 ? false : console.log(index));


map

Returns a new object or array by through and running a callback to accept or reject each iteration. Returning false rejects item. Returning undefined stops the loop. Any other return value will be set on the object or array under the current key/index.

map(Array|Object: array, Function: callback, ?Mixed: this, ...args): Void
// Loop an array
// [0,1,2]
let arr = map(['one','two','three'], (index, value) => index);

// {foo: 'foo', bar: 'bar'}
map({foo: 'one', bar: 'two'}, (key, value) => key);

// ['two']
let arr = map(['one','two'], (index, value) => value !== 'one' ? value : undefined);


array_unique

Filters out non-unique values from an array.

array_unique(Array|Object: array): Array
// [0,1,2,3]
let arr = array_unique([0,1,2,3,3]);


array_filter

Filters out empty items from an array or object and returns a new Array or Object.
Does not modify the original Array or Object, rather it will filter out empty items and return a new Object or Array.

array_filter(Object|Array: array): Object|Array
// [0, 1, 2, 3, 4, 5]
let array = [0, 1, 2, null, undefined, 3, 4, '', 5];
array = array_filter(array);

// [0, 1, 2, 3, 4, 5, 6]
let array = [0, 1, 2, null, undefined, 3, 4, '', [], 5, {}, 6];
array = array_filter(array);


array_set

Sets a value to array or object using dot.notation.
Sets key/value an pair to any nested array or object via dot.notation. If parent items do not exist in the path, they are created.

array_set(String: path, Mixed: value, Object|Array: array): Void

To set an array key, use the array index with brackets [num]. For Objects simply use the Object key and a dot .key:

let array = [
    {foo: 'foo'},
    {bar: 'bar'}
];

/*
[
    {foo: 'bar'},
    {bar: 'bar'}
];
*/
array_set('[0].foo', 'bar', array);
let obj = {
    foo: [1,2,3],
    bar: [4,5,6]
};

/*
{
    foo: [9,2,3],
    bar: [4,5,6]
};
*/
array_set('foo[0]', 9, obj);

On nested nested Arrays/Objects if the path does not exist it will be created automatically.

// [0, 1, 2, 3, 4, 5]
let obj = {
    foo: [
        { bar: { baz: [1, 2, 3] } }
    ]
};

/*
{
    foo: [
        { bar: { baz: [1, 9, 3], foo: [3] } }
    ]
};

*/
array_set('foo[1].bar.baz[1]', 9, obj);

array_set('foo[1].bar.foo[1]', 3, obj);


array_get

Returns an item from an array or object using dot.notation.
Returns the item indexed by the path or undefined if it does not exist.

array_get(String: path, Object|Array: array): Mixed
let array = [ {foo: 'foo'} ];

let foo = array_get('[0].foo', array);

To access an array key, use the array index with brackets [num]. For Objects simply use the Object key and a dot .key:

let array = [
    {foo: 'foo'},
    {bar: 'bar'}
];

// foo
let foo = array_get('[0].foo', array);
let obj = {
    foo: [1,2,3],
    bar: [4,5,6]
};

// 1
let one = array_get('foo[0]', obj);

array_get will work on deeply nested Arrays/Objects without having to run validation that the item exists first.

let obj = {
    foo: [
        { bar: { baz: [1, 2, 3] } }
    ]
};

// Returns 2
let two = array_get('foo[0].bar.baz[1]', obj);

// Returns undefined
let undef = array_get('foo[0].bar.baz[15]', obj);


array_has

Checks if an item from an array or object exists using dot.notation and returns a boolean.

array_has(String: path, Object|Array: array): Boolean
let array = [ {foo: 'foo'} ];

// true
array_has('[0].foo', array);


array_delete

array_has(String: path, Object|Array: array): Void
let array = [ {foo: 'foo'} ];

// No need re-assign here as the original array is now modified.
array_delete('[0].foo', array);


in_array

Checks if an item exists in array or object.

in_array(Mixed: needle, Object|Array: array, ?Boolean: strict = false): Void
let array = [ 1, 2, 3 ];

// true
in_array(2, array);

// false
in_array(5, array);

When passing strict as true, values must be the same

let obj = { foo: 'bar' };
let array = [obj];

// true
in_array(obj, array);

// true
in_array({ foo: 'bar' }, array);

// false
in_array({ foo: 'bar' }, array, true);