@felix-ongom/mixture
v1.0.0
Published
a simple library for removoving, inserting and moving eliements in an array given the indecies
Downloads
5
Readme
mixture
This is a javascript array library, simple and easy to use, It supports all kinds of array. it have methods like;
removeElement()
This is a method that takes two parameters,
- first parameter being the array and the second parameter being
- the index of the ement you want to remove from the array
- it returns an array missing one item
removeElement([1,2,3], 1)
//[1,3]
insertElement()
It is a method that takes three parameters,
- -first parameter being the array
- -the second parameter is the elemrnt you want to insert
- -third parameter is the index where you want it to appear
- -it inserts the element at the end of the array if the index supplied is greater than or equal to the array length
- it returns an array with one more element */
insertElement([1,2,3],20, 1)
//[1, 20, 2, 3]
insertElement([1,2,3],{num:20}, 10)
//[1, 2, 3, {num:20}]
moveElement()
It is a method that takes three parameters,
- -first parameter being the array
- -the second parameter is index of the elements you want to move
- -third parameter is the index where you want it to appear
- -it moves the element to the end of the array if the index supplied is greater than or equal to the array length */
moveElement([1, {two:2}, 3, 4, 5], 1, 3)
//[1,3,4,{two:2},5]
moveElementAcross(),
This is a method that takes two parameters,
- -first parameter is an object with index of the element you are removing and array you are removing from.
- -second parameter is an object with index you are inserting to and array you are inserting to
- -it moves the element to the end of the array if the index supplied is greater than or equal to the array length in the second parameter
- it returns an array of two array, firts one missing one element and
- second one with one element more, */
moveElementAcross(
{index:1, arr1:[1,2,3,4]}
{index:2, arr1:['one', 'two', 'three', 'four']}
)
//[
[2,3,4],
['one', 'two', 1, 'three', 'four']
]
randomChance()
Given the array
let arr = [0,1,2,4,6]
-randomChance takes in one mandatory array paramerter and return array with the same length
but with elements place randomly. Elements will appear by chance
-Note that some elements may appaer more thn once meanwhile other elements will not appear completely.
-it takes in the second parameter which is an inter number and it is the length of the result that you want back. if you don't supply it default to your array lenth.
-it takes in the third paramerter which is an array, The first element beng index of an element, and the second element being how much you want it to appear or dominate in the result and it ranges from 0 to 1,
let arr = [1,2,3,4,5,6,7,8,9,10]
randomChance(arr, 2)
//[2,4]
randomChance(arr, 10,[2, 0.2] )
//[3,5,3,4,5,6,8,6,7,10]
- It can also take an array of array, [[0, 0.2], [3,0.4]], this mean you want elements at indeces 0 and 3 to dominate the result by 0.2 and 0.4 respective
let arr = [1,2,3,4,5,6,7,8,9,10]
randomChance(arr, 3) //[3,3, 1]
randomChance(arr, 5, [0, 0.2]) //[1, 4, 1, 3, 7]
randomChance(arr, 8, [[0,0.2],[3, 0.3]]) //[1, 4, 1, 4, 4 , 7, 3,10]
randomChoice()
It returns the same array with elements placed randomly with each element appearing once.
- -takes in two parameter,
- first paramert is mandatory and is an array
- second paramert is optionanl and is the length of the array that you want back
let arr = [1,2,3,4,5]
randomChoice(arr, 4) //[5,3,4,1,2]
randomChoice(arr, 4) //[3 4,2,5]
randomChoice(arr, 4) //[5,2,4,3]
sort()
This method takes in two mandatory parameter ,first is an array of object and second is an objsct ,{id:1} describing how you want to sort.
let arr =[num:'one', num:'two', three:'three']
sort(arr, {num:1})
sort(arr, {num:-1})
- -the key of the object is the field you want to look at when sorting and the value is either 1 or -1, 1 means ascending order while -1 means descending order.