import '@pim.sk/utils/array-extend.mjs'
import '@pim.sk/utils/array-extend.mjs' before usage.
If not loaded then .sum, .avg, .unique, ... return undefined without throwing errors.
Example:
[1 2 3].sum -> undefinedRemove duplicate items from the array.
[1, 2, 3, 2, 4, 5, 5]
const uniqueItems = items.unique
Find duplicate items in the array.
[1, 2, 3, 2, 4, 5, 5]
const duplicatesItems = items.duplicates
Find items that do not have a pair in the array.
a = [1, 2, 3, 4, 5]
b = [1, 2, 4, 5]
const items = a.concat(b)
const unpairedItems = items.unpaired
Sum the values of the array.
[1, 2, 3, 4, 5]
const sumItems = items.sum
Average the values of the array.
[1, 2, 3, 4, 5]
const avgItems = items.avg
Get the numbers from the array.
[1, "2a", 3, null, 4, 5, "hello"]
const numbersItems = items.numbers
Get the non-numbers from the array.
[1, "2a", 3, null, 4, 5, "hello"]
const nonumbersItems = items.nonumbers
Shuffle the array.
[1, 2, 3, 4, 5]
const shuffleItems = items.shuffle
Return the first occurrence from the associative array.
[ {a:1,b:"one"}, {a:2,b:"two"}, {a:3,b:"one"} ]
const getItems = items.get("b", "one")
Return all occurrences from an associative array based on a condition.
[ {a:1,b:"one"}, {a:2,b:"two"}, {a:3,b:"one"} ]
const getsItems = items.gets("b", "one")
const items = [ {a:1,b:"one"}, {a:2,b:"two"}, {a:3,b:"one"} ]Clean the array.
items.clean()
Delete the first occurrence from the associative array.
items.delete("b", "one")
1/ Search for an element in the array where element[key] === item[key] — i.e. compares the value of the identification key 2/ If found → replaces it with a new item via splice 3/ If not found → adds the item to the end via push
items.put( "a", {a:3,b:"three"} )
items.put( "a", {a:4,b:"four"} )
Bulk insert items : array of items to insert boolean : true = delete existing array before inserting (replace), false = add to the end (append)
items.puts( [ {a:4,b:"four"}, {a:5,b:"five"} ] )
items.puts( [ {a:4,b:"four"}, {a:5,b:"five"} ], true )
bulk upsert by key key : name of property that serves as identifier (e.g. "id") items : array of objects to process - if element with given key exists → replace it (update) - if it does not exist → add it to the end (insert)
items.putsBy( "a", [ {a:3,b:"three"}, {a:4,b:"four"} ] )
Remove duplicates based on the specified key, keep the LAST occurrence key : name of the property by which duplicates are compared
items.uniqueBy( "b" )
Move within array - change index from : from index to : to index * it is more efficient than 2x splice
items.move( 0, 2 )