This is the light weight array package with fun array manipulations and methods.
npm install --save @jitiendran/funarray
Or if you're not into package management, just download a ZIP file.
After installing the package first the require the package from node modules into your javasctript file
const { FunArray } = require("@jitiendran/funarray");
Now, you can create a empty array by creating a object for the class FunArray.
let array = new FunArray();
You can also add element to array array while creating itself by doing the below.
let array = new FunArray(1, 2, 3, 4);
let fruits = new FunArray("Apple", "Bannana", "Orange");
Or
let fruits = new FunArray();
fruits.push("Apple");
fruits.push("Bannana");
let fruit = fruits.atIndex(0); //returns value
Or
const index = fruits.indexOf('Apple') //returns index
let fruit = fruits[index]
for (let i = 0; i < fruits.length(); i++) {
console.log(fruits.atIndex(i));
}
Output
Apple
Bannana
Orange
fruits.pop(); //removes last item
console.log(fruits.array()); //converts funarray to array
Output
[ 'Apple', 'Bannana' ]
fruits.shift(); //removes Apple from front
fruits.unshift("Strawberry");
//output: ['Strawberry','Bannana','Orange']
Or
fruits.unshift('Strawberry','PineApple',...)
fruits.removeAt(0); //removes the elements at index 0
fruits.remove("Apple"); //removes apple
let copy = fruits.slice();
fruits.print(); //prints the array
const str = fruits.toString();
fruits.trash();
fruits.fill("Avacado"); //fills everything with Avacado
Or
fruits.fill("Avacado", 0, 1); //fills Avacado starting from index 0 upto 1
let vegetables = new FunArray("Tomato", "Onion", "Brinjal");
fruits.merge(vegetables);
fruits.print();
Output
[ 'Apple', 'Bannana', 'Orange', 'Vegetables', 'Onion', 'Brinjal' ]
let num = new FunArray(1, 2, 3, 3).distinct(); //returns normal array
console.log(num);
//output [1,2,3]
let users = new FunArray(
{
_id: 1,
Username: "Albert",
},
{ _id: 2, Username: "Jason" }
);
users.removeWhere("_id === 1"); //expression should be string
console.log(users.array());
Output
[ { _id: 2, Username: 'Jason' } ]
let users = new FunArray(
{
_id: 1,
Username: "Albert",
},
{ _id: 2, Username: "Albert" },
{ _id: 3, Username: "Jason" }
);
let foundUsers = users.findWhere("Username === Albert"); //returns normal array
console.log(foundUsers);
Output
[ { _id: 1, Username: 'Albert' }, { _id: 2, Username: 'Albert' } ]
const num = new FunArray(4, 2, 3, 3);
let users = new FunArray(
{
_id: 2,
Username: "Albert",
},
{ _id: 1, Username: "Albert" },
{ _id: 3, Username: "Jason" }
);
num.sortAsc();
users.sortAsc("_id"); //sort by any key;
num.print();
users.print();
Output
[ 2, 3, 3, 4 ]
[
{ _id: 1, Username: 'Albert' },
{ _id: 2, Username: 'Albert' },
{ _id: 3, Username: 'Jason' }
]
const num = new FunArray(4, 2, 3, 3);
let users = new FunArray(
{
_id: 2,
Username: "Albert",
},
{ _id: 1, Username: "Albert" },
{ _id: 3, Username: "Jason" }
);
num.sortDesc();
users.sortDesc("_id"); //sort by any key;
num.print();
users.print();
Output
[ 4, 3, 3, 2 ]
[
{ _id: 3, Username: 'Jason' },
{ _id: 2, Username: 'Albert' },
{ _id: 1, Username: 'Albert' }
]
const names = new FunArray("Albert", "Albert", "Jason");
names.replace("Albert", "Happy"); // By default occurence is 1
console.log(names.array());
names.replace("Albert", "Happy", 2); //specifying occurence
console.log(names.array());
Output
[ 'Happy', 'Albert', 'Jason' ]
[ 'Happy', 'Happy', 'Jason' ]
const names = new FunArray("Albert", "Albert", "Jason");
names.replaceAll("Albert", "Happy");
Output
[ 'Happy', 'Happy', 'Jason' ]
const names = new FunArray("Albert", "Albert", "Jason");
names.funPush("James", "Elric", "Alphonse");
names.print();
Output
[ 'Albert', 'Albert', 'Jason', 'James', 'Elric', 'Alphonse' ]
const names = new FunArray("Albert", "Albert", "Jason");
names.findAndUpdate(0, "Alphonse");
names.print();
Ouput
[ 'Alphonse', 'Albert', 'Jason' ]