Simple array list implementation in Javascript like in Java ,Only 8kb size.
When called API,as below scene:
index
is out of bounditem
has not unique key- add an reduplicated or remove unExisted item
it will fail and throw corresponding error which helps developers to catch and konw what happens
Install
$ npm install ins-array-list --save
Usage
const ArrayList=const mData= id:1 name:'Alicy' gender:2 id:2 name:'Anna' gender:2 id:3 name:'Tom' gender:1 //ArrayList(listData,unqiueKey) unqiueKey default "id"// const mList=new ArrayList(mData)const mList=mData'id'//do something by api below ... //get list data after each api operation if wantedconsole// ...more API as below
API
.add(item)
Type:Object item
It adds item to the end of the array list
const item= id:4 name:'Steve' gender:1 mList//=>true
.add(index,item)
Type:Number index
Type:Object item
It inserts item to the index of the array list
const item= id:4 name:'Steve' gender:1 mList//=>true
.addAll(items)
Type:Object items
It adds all items to the array list
const items= id:4 name:'Steve' gender:1 id:5 name:'Miya' gender:2 mList//=>true
.addAll(index,items)
Type:Number index
Type:Object items
It inserts items to the index of the array list
const items= id:4 name:'Steve' gender:1 id:5 name:'Miya' gender:2 mList//=>true
.remove(index)
Type:Number index
It removes the item at index of the array list.
//say mList.size()==5 mList//=>true
.remove(item)
Type:Object item
It removes the item in the array list.
//say mList.size()==5 const item= id:4 name:'Steve' gender:1 mList//=>true
.removeAll(sublist)
It removes all items of sublist in the array list
//say mList.size()==5 const subList= id:1 name:'Alicy' gender:2 id:3 name:'Tom' gender:1 id:4 name:'Steve' gender:1 mList//=>true
.removeIf(predicator)
Type:Function(item,index)
It removes the items that meets predicator.
//remove then gender==1 mList
.sort(predicator)
Type:Function(a,b)
It sorts the items that meet the predicator.it used like normal arr.sort((index,i)=>{...}) but inner implementation is sorted by shell method,which make it sort highly efficient
//sortBy gender:item with gender of 1 is before one with gender of 2 mList
.iterate(iterator)
Type:Function(item,index)
It iterates all the elements in the array list and then do by iterator
//sort by gender mList
.shuffle()
It shuffles the elements in the array list
mList
.subList(formIndex, toEndIndex)
Type:Number
Type:Number
It returns new child array without change source
//say mList.size()==5 const newItems=mList console//=>3 mListsize//=>5
.contains(item)
Type:Object
It returns whether the arrray list contains the item judge by the uniqueKey
mList//=>true
.isEmpty()
It returns whether the arrray list is empty
mList//=>false mListclear mList//=>true
.clear()
It removes all the items in the array list.
mList//=>false mListclear mList//=>true
.indexOf(item)
Type:Object item
It returns the index of item in the array list
mList//=>1
.get(index)
Type:Number index
It gets the element at index in the array list
mList//=>{id:2,name:'Anna',gender:2}
.set(index,newItem)
Type:Number index
Type:Object newItem
It sets the newItem at index in the array list
mList
.size()
It returns the length of the array list
mListsize//=>3
.source
It returns the array list
License
MIT © insonghua2.