Array and Array operation, searching and iteration in javascript

Array are used to store multiple value in a single variable.It can hold multiple value in one time

For example:Storing the list of brand in single list of item be like this:

var brand1=”lancer”;

var brand2=”puma”;

var brand3=”lee cooper”;

var brand4=”reebok”;

In a single name you can hold multiple value by using array and by using the index number you can access these value.In array a declaration can be single or multiple lines.

Space and line breaks are not important in array:

Syntax of arrya:

var   array_name= [brand1, brand2, brand3, brand4 ……];

New javascript keywords for assign value

Array Operation

Array operation is used to allow  to access multiple value in single array.Array operation are supporting single and multiple dimension of array.

Array Functions and their description

1. Array() :

 It is used to create an array in Javascript.

var arr = Array();
arr.push("Hello");
arr.push("Hi");
console.log(arr);
Output:
["Hello", "Hi"]
 
 
3. toString()

It concatnate array elements into a comma seperated string.

var str = arr.toString();
console.log(str);

Output:

Hello, Hi

3.  fill()

 It is used to fill or replace the array value with the given value.

Syntax is: <Array>.fill(<New Value>,Start Position, End Position);

var arr = ["Lucknow", "Kanpur", "Delhi", "Noida"];
arr.fill("Allahabad",1,3);
console.log(arr);

Output:
["Lucknow", "Allahabad", "Allahabad", "Noida"]

3. filter()

This function is used filter the elements of array.

Lets have an example:

var arr = ["Lucknow", "Kanpur", "Delhi", "Noida"];
var newarr = arr.filter(function(val){
	return val == "Kanpur";
});
console.log(newarr);

Output:

["Kanpur"]

Lets take a numeric example:

var numarray = [1,10,15,7,3,8,20,11,16,6];
var newarr = numarray.filter(function(val){
	return val>10;
});
console.log(newarr);

Output:

[15, 20, 11, 16]

In ES6 format:

var numarray = [1,10,15,7,3,8,20,11,16,6];
var newarr = numarray.filter(val => val>10);
console.log(newarr);
4. find()

This function is used find the elements of array. It is like the filter but it stop seraching after find first matching element. If value if not available in array then it returns undefined.

Lets have an example:

var arr = ["Lucknow", "Kanpur", "Delhi", "Noida"];
var newarr = arr.find(function(val){
	return val == "Kanpur";
});
console.log(newarr);

Output:

Kanpur

Lets take a numeric example:

var numarray = [1,10,15,7,3,8,20,11,16,6];
var newarr = numarray.find(function(val){
	return val>10;
});
console.log(newarr);

Output:

15

In ES6 format:

var numarray = [1,10,15,7,3,8,20,11,16,6];
var newarr = numarray.find(val => val>10);
console.log(newarr);

 

5. findIndex()

This function is used find the element index of array. It is like the find but it returns first matching element's index. If value if not available in array then it returns -1.

Lets have an example:

var arr = ["Lucknow", "Kanpur", "Delhi", "Noida"];
var pos = arr.findIndex(function(val){
	return val == "Kanpur";
});
console.log(pos);

Output:

1

In ES6 format:

var numarray = [1,10,15,7,3,8,20,11,16,6];
var pos = numarray.findIndex(val => val>10);
console.log(pos);

Output:

2

 

6. indexOf()

This function is used find the element index of array. It is like the find but it returns first matching element's index. If value if not available in array then it returns -1.

The main difference is in findIndex and indexOf is  indexOf takes value as parameter to search weather findIndex take search function as paramenter.

In findIndex you can give second paramenter to tell from where it will start search.

Lets have an example:

var arr = ["Lucknow", "Kanpur", "Delhi", "Noida"];
var pos = arr.indexOf("Kanpur");
console.log(pos);

Output:

1

 

7. shift()

Thisfunction is used to remove first element of array. It returns the removed element of array. If array is empty then it return undefined.

var arr = ["Lucknow", "Kanpur", "Delhi", "Noida"];
var removed = arr.shift();
console.log(removed);
console.log(arr);

Output:

Lucknow

["Kanpur", "Delhi", "Noida"]

 

8. unshift()

This function is used to add one or more elements in array at top. It returns counting of elements.

var arr = ["Lucknow", "Kanpur", "Delhi", "Noida"];
var added = arr.unshift("Allahabad", "Varanasi");
console.log(added);
console.log(arr);

Output:

6

["Allahabad", "Varanasi", "Lucknow", "Kanpur", "Delhi", "Noida"]

 

9. slice()

This function is used to get some elements in array format form array. It takes 1 or 2 paramenter- First is start index and second is optional that is end index.

var arr = ["Lucknow", "Kanpur", "Delhi", "Noida"];
var newarr = arr.slice(1, 3);
console.log(newarr);

Output:

["Kanpur", "Delhi"]

 

10. splice()

This function is used to update the elements of array. By using this method you can insert/update/remove element from any index of array.

var arr = ["Lucknow", "Kanpur", "Delhi", "Noida"];

// Add new element at second position.
arr.splice(1, 0, "Allahabad");
console.log(arr);

// Remove Third element of array
arr.splice(2, 1);
console.log(arr);

// Update Second element of array
arr.splice(1, 1, "Varanasi");
console.log(arr);

Output:

["Lucknow", "Allahabad", "Kanpur", "Delhi", "Noida"]
["Lucknow", "Allahabad", "Delhi", "Noida"]
["Lucknow", "Varanasi", "Delhi", "Noida"]

 

11. forEach()

This function is used to traverse one by one element and provides the element in callback.

var arr = ["Lucknow", "Kanpur", "Delhi", "Noida"];
arr.forEach(function(element){
    console.log(element);
});

Output:

Lucknow
Kanpur
Delhi
Noida

 

12. map()

This function is used to create new array with current array by applying a function.

var arr = [1,2,3,4];
var newarr = arr.map(function(element){
    return element * 5;
});
console.log(newarr);

Output:

[5, 10, 15, 20]

 

12. reduce()

This function is used to accumulate each element of the array by reducer function.

var arr = [1,2,3,4];
var value = arr.reduce(function(sum, val){
    return sum + val;
});
console.log(value);

Output:

10

 

13. reduceRight()

This function works as reduce function but traverse from right to left.

 

14. push()

This function is used to add new element in array end.

var arr = [1,2,3,4];
arr.push(5);
console.log(arr);

Output:

[1, 2, 3, 4, 5]

 

15. pop()

This function is used to remove last element of array.

var arr = [1,2,3,4];
arr.pop();
console.log(arr);

Output:

[1, 2, 3]

 

16. join()

This function is used to join all elements of array with given seperator.

var arr = ["Lucknow", "Kanpur", "Delhi", "Noida"];
var str = arr.join(", ");
console.log(str);

Output:

Lucknow, Kanpur, Delhi, Noida

 

17. keys()

This function is used to get all key index of array in Array Iterator format .

var arr = ["Lucknow", "Kanpur", "Delhi", "Noida"];
var keys_iterator = arr.keys();
for (let key of keys_iterator) {
  console.log(key); // expected output: 0 1 2
}

Output:

0
1
2
3

17. sort()

This function is used to sort elements of array.

var arr = ["Lucknow", "Kanpur", "Delhi", "Noida"];
arr.sort();
console.log(arr);

Output:

["Delhi", "Kanpur", "Lucknow", "Noida"]

 

 

 

 

 

Keywords: