Useful methods in Arrays in Java Script

Useful methods in Arrays in Java Script

Why we need arrays and what is the use?

  • We have so many number of employees and we need to store their data so that for storing purposes we need arrays.

In array methods you don't know how to use no worries here I can tech you regular usage and simplest way and easily so that you can understand easily.

For emample I am creating an array with the help of const(const values once declared never change)

const emp = ["Vinod", "Rajesh", "Suresh", "Jay"];
console.log(emp)
output-[ 'Vinod', 'Rajesh', 'Suresh', 'Jay' ]

Another method to create an array with new Keyword.

const emp = new Array("Vinod", "Rajesh", "Suresh", "Jay");

Finding the length in array you can use the length method.

  • Here for knowing the length of the array we can use length method.

Ex: I want to know the array length

const emp = ["Vinod", "Rajesh", "Suresh", "Jay"];
console.log(emp.length)
output- array length is 4.

Accessing an array(Array values always starts with zero)

  • You want to access the array you can simple use variable name with [] index value.

Ex: I want to access suresh data

const emp = ["Vinod", "Rajesh", "Suresh", "Jay"];
console.log(emp[2]);
output- Suresh

Adding the array in existing array data

  • here we are using the push method to add the array data and it moves to last data name.

Ex: See below code

const emp = ["Vinod", "Rajesh", "Suresh", "Jay"];
const newEmp = emp.push("Hemanth");
console.log(emp);

Output- [ 'Vinod', 'Rajesh', 'Suresh', 'Jay', 'Hemanth' ]
Here Hemanth is newly added.

Remove the Last element in array existing array data

  • Here we are using the pop method to remove the last array element.

Ex: See below code

const emp = ["Vinod", "Rajesh", "Suresh", "Jay", "Hemanth"];
const remEmp = emp.pop();
console.log(emp);

Output- [ 'Vinod', 'Rajesh', 'Suresh', 'Jay' ]
Here Hemanth is removed from the array list.

Remove the First element in array existing array data

  • Here we are using the shift method to remove the first array element.

Ex: See below code

const emp = ["Vinod", "Rajesh", "Suresh", "Jay", "Hemanth"];
const remEmp = emp.shift();
console.log(emp);

Output- [ 'Rajesh', 'Suresh', 'Jay', 'Hemanth' ]
Here Vinod is removed from the array list.

Add a new element in first array in existing array data

  • Here we are using the unshift method to remove the first array element.

Ex: See below code

const emp = ["Vinod", "Rajesh", "Suresh", "Jay", "Hemanth"];
const remEmp = emp.unhift("Namresh");
console.log(emp);

Output- [ 'Naresh', 'Vinod', 'Rajesh', 'Suresh', 'Jay', 'Hemanth' ]
Here Naresh is added in first array list.

Removing single element in array existing array data.

  • Here we are using the splice method to remove the single array element.

Ex: Here I want to remove Hemanth you can see the below code.

const emp = ["Vinod", "Rajesh", "Suresh", "Jay", "Hemanth"];
const remEmp = emp.splice(4);
console.log(emp);

Output- [ 'Vinod', 'Rajesh', 'Suresh', 'Jay' ]
Here Hemanth is removed in array list.

> In Splice menthod you want to remove single element you know the index of array you can use splice(4) inside brackets array index.

Removing multiple element in array existing array data.

  • Here we are using the splice method to remove the multiple array element.

Ex: Here I want to remove Rajesh to Jay you can see the below code.

const emp = ["Vinod", "Rajesh", "Suresh", "Jay", "Hemanth"];
const remEmp = emp.splice(1, 3);
console.log(emp);

Output- [ 'Vinod', 'Hemanth' ]
Here Rajesh to Jay is removed in array list.

> In Splice menthod you want to remove multiple element you know the index of array you can use splice(1, 3) inside brackets array index here 1 is start and 3 is end for the delete the array elements.

Combined multiple arrays.

  • Here Combined multiple arrays we are using the concat method to merge the multiple array list.

Ex: Here I want to combine 2 arrays.

const firstData = ["Vinod", "Rajesh", "Suresh", "Jay", "Hemanth"];
const SecondData = ["Sirisha", "Rani", "Kavitha", "Aamani", "Hema"];

const merge = firstData.concat(SecondData)
console.log(merge);

Output- [
  'Vinod',   'Rajesh',
  'Suresh',  'Jay',
  'Hemanth', 'Sirisha',
  'Rani',    'Kavitha',
  'Aamani',  'Hema'
]
Here you can see the output 2 combined array list.

Copy of Array .

  • Here we can copy an array we can use the spread operator(...) method to copy the array list.

Ex: See below code

const firstData = ["Vinod", "Rajesh", "Suresh", "Jay", "Hemanth"];
const copy =[...firstData]

console.log(copy);

Output of copy is- [ 'Vinod', 'Rajesh', 'Suresh', 'Jay', 'Hemanth' ]
Here you can use multiple type of copy methods ex: Array.from(firstData) or firstData.splice(firstData) feel free to use any of methods.

Sorting an Array .

  • Here we can sort an array with the help of sort method.

Ex: Here we can sort the array in Alphabetic order.

const firstData = ["Vinod", "Rajesh", "Suresh", "Jay", "Hemanth"];
console.log(firstData.sort());

Output is- [ 'Hemanth', 'Jay', 'Suresh', 'Rajesh', 'Vinod' ]

>In abouve you can see the Array list in Alphabetic Order.

Reverse an Array .

  • Here we can Reverse the array with the help of reverse method.

Ex: Here we can sort the array in Alphabetic order.

const firstData = ["Vinod", "Rajesh", "Suresh", "Jay", "Hemanth"];
console.log(firstData.reverse());

Output is- [ 'Hemanth', 'Jay', 'Suresh', 'Rajesh', 'Vinod' ]

>In abouve you can see the Array is in reverse Order.

Thanks for reading