-
extends
Array
-
native + custom methods
-
register callbacks on
push
,pop
,shift
,unshift
-
observe array on
push
,pop
,shift
,unshift
- Get the package through CDN
<script src="https://cdn.jsdelivr.net/gh/Droid997/custom-array@1.0/src/customArray.min.js"></script>
- It will be placed in the window object
window.customArray
var arr = new customArray();
//returns arr with length 0
var arr = new customArray(size?,value?);
// size default = 0
var arr = new customArray(5,1);
// arr => [1,1,1,1,1]
size | optional, default = 0;
value | optional;
array = []
fills array with the passed value
value | default = 0
array with filled values
var arr = new customArray(5);
arr.initialize(1);
// arr => [1,1,1,1,1]
extends the array length
newlen | default = current array length
array with extended length
var arr = new customArray(5);
// arr.length => 5;
arr.extend(10)
// arr.length => 10;
reduces array size
newlen | default= current array length
array with reduced length
var arr = new customArray(10);
// arr.length => 10;
arr.reduceSize(5)
// arr.length => 5;
clears all the values of the array
cleared array
var arr = new customArray(10,10);
// arr[0] => 10;
arr.clear()
// arr[0] => undefined;
returns the value at passed index
index
value at specified index
var arr = new customArray(10,10);
// arr[0] => 10;
arr.clear()
// arr[0] => undefined;
inserts elements's at specified index
index => index at which element should be inserted
items => Array of items
array
var arr = new customArray(5,5);
// arr => [5,5,5,5,5]
arr.insertAt(2,"a","b","c");
// arr => [5,5,a,b,c,5,5,5]
remove elements from array
index => index at which element should be removed
count | default=1 => Number of elements to be removed from index
array
var arr = new customArray(5,5);
// arr => [5,5,5,5,5]
arr.remove(2);
// arr => [5,5,5,5]
Event is fired when one of the following occours
-
push
-
pop
-
shift
-
unshift
function => function to be called when event occours
function callback takes
array
andelement
as parameters
array
var arr = new customArray(5);
arr.observe(function(array,element){
})
arr.push(2);
arr.observe(function(array,element){
//control here
//array => [5]
//element => 5
})
Removes observe
callback if present
true
var arr = new customArray(5);
arr.observe(function(array,element){
})
arr.push(2);
arr.unobserve()
Allows to register custom events on one of the following
-
push
-
pop
-
shift
-
unshift
Note: First observe event is fired first and based on the event type registred function gets called
event => can be one of the following
push
,pop
,shift
,unshift
function => function to be called when event occours
function callback takes
array
andelement
as parameters
boolean
var arr = new customArray(5);
arr.registerEvent('push',function(array,element){
})
arr.push(2);
arr.registerEvent('push',function(array,element){
// control here
// array => [2]
// element => 2
})
Removes registerEvent
callback for event
specified if present any.
boolean
var arr = new customArray(5);
arr.registerEvent('push',function(array,element){
})
arr.push(2);
arr.unregisterEvent("push")
fills the array with given value from start index to end index .
value => any value to be filled. start => start index | default = 0. end => end index | default = array length.
array
var arr = new customArray(5);
arr.fill(2);
arr => [2 ,2 ,2 ,2 ,2]
returns the sum of numbers in the array .
sum
var arr = new customArray(5,2);
arr => [2 ,2 ,2 ,2 ,2]
arr.push("str");
arr => [2 ,2 ,2 ,2 ,2,"str"]
var sum = arr.sum();
sum => 10
index at which the value should be replaced at.
array
var arr = new customArray(5,2);
arr => [2 ,2 ,2 ,2 ,2]
arr.replace(2,"5");
arr => [2 ,2 ,5 ,2 ,2]
copy the current array.
from => from index | default = 0 to => to index | default = array length
copied array
var arr = new customArray(5,2);
arr => [2 ,2 ,2 ,2 ,2]
var copiedArray = arr.copy();
copiedArray => [2 ,2 ,2 ,2 ,2]
finds the total number of occurrences of the passed element.
element => element to find the count of.
count
var arr = new customArray(5,2);
arr => [2 ,2 ,2 ,2 ,2]
var count = arr.count(2);
count => 5