Arrays in JS
Properties:
Constructor
length - a number to denote the number of elements in an array.
let arr1=[1,2,3,[4,5] ] ; console.log(arr1.length) ; will print 4 .
Accessing Elements in an Array:
i) Access through index
ii) Adding elements:
a) To the end of the array: Array.push(element); -> return the length of the new array.
let arr2=Array[];
b) In the beginning of the array. Array.unshift(elements_to_add); -> returns new length.
let arr=[1,2,3,[4,5]];
console.log(arr.length);
let arr1=Array();
let len=arr1.push('a','b'); // push element to the end.
console.log(len);// output is 2
len=arr1.unshift('z','y');
console.log(len,arr1);// output is 4 [ 'z', 'y', 'a', 'b' ]
c) Inserting elements in any position of the array.
Array.splice(Index position from where to insert, no.of elements to remove from while
inserting, <list of elements to be inserted>)
arr1=[1,2,3,['l','a']];
let deleted=arr1.splice(1,2,'d','e');
console.log(deleted,arr1); // output is [ 2, 3 ] [ 1, 'd', 'e', [ 'l',
'a' ] ] and deleted elements are returned as array
What will be the value of deleted , if deleted = arr1.splice(1,0,’z’ );’
iii) Removing elements from an Array:
a) Remove the last element ele=Array.pop(); -> returns the popped out element or
undefined if no elements in the array.
b) Removing the first element in the Array ele=Array.shift() -> returns the first element of
the array and shifts all the other elements one place prior. Or undefined if array is empty.
c) Removing some elements from the array.
Same splice method but without the third argument.
arr1=[ 1, 'd', 'e', [ 'l', 'a' ] ]
console.log(arr1.splice(2,1)); // output is [‘e’] as array.
iv) Copying elements to another array,
Array.slice( starting index position, ending position ) - > returns sliced elements as array.
let scores=[10,20,21,25,26,28,39];
let sliced=scores.slice(2,4);
console.log(sliced); // output is [ 21, 25 ]
slice () takes +ve and -ve values as arguments and also one or two arguments.
slice( +ve, +ve ) - denotes starting index, ending index
slice( +ve) - slice the elements from the specified index to the last.
slice(-ve) - slices the array from the position indexed from the last.
slice(+ve, -ve) - slices the array from the given position to the end position indexed from the last.
let scores=[10,20,21,25,26,28,39];
let sliced=scores.slice(2,4);
console.log(sliced);// output is [ 21, 25 ]
console.log(scores.slice(2));// output is [ 21, 25, 26, 28, 39 ]
console.log(scores.slice(-3));// output is [ 26, 28, 39 ]
console.log(scores.slice(2,-3));// output is [ 21, 25 ]
v) Sorting and reversing an array. // This is an mutate function which changes the calling
array object also.
scores=[3,5,3,2,6,8,4,2,1];
console.log(scores.sort()); //output is [ 1, 2, 2, 3, 3, 4, 5, 6, 8]
console.log(scores.reverse()); // output is [8, 6, 5, 4, 3, 3, 2, 2, 1 ]
In ES2023 toSorted and toReversed() methods return the sorted and reversed array to a new
array without altering the original array.
vi) Finding elements in an array:
i) indexOf: returns the index position of the element occurring first time from the given index
position.
scores=[3,5,3,2,6,8,4,2,1];
console.log(scores.indexOf(2)); // output is 3 from the beginning.
console.log(scores.indexOf(2,4)); // output is 7. from 4th position 2
occurs at 7
console.log(scores.indexOf(20)); // output is -1 to indicate no occurrence
of 20
ii) lastIndexOf : returns the position of last occurrence of the given element.
scores=[3,5,3,2,6,8,4,2,1];
console.log(scores.lastIndexOf(2)); // output is 7
console.log(scores.lastIndexOf(5,4)); // output is 1. from 4th position 2
occurs at 3
console.log(scores.lastIndexOf(20)); // output is -1 to indicate no
occurrence of 20
iii) By search condition
a) findIndex( function,<optional arguments>) ;
b) scores=[1,3,5,2,6,5,8,9];
c) console.log(scores.findIndex(num=>num%2!==0) ); // ouput is 0
d) console.log(scores.findIndex(num=>num%2==0) ); // output is 3
e)
b) findLast ( function, < optional arguments>) - returns the last element that passes the
test condition.
scores=[1,3,5,2,6,5,8,9];
console.log(scores.findLast(num=>num%2!==0) ); // ouput is 9
console.log(scores.findLast(num=>num%2==0) ); // output is 8
Similarly learn about findLastIndex() ???//returns the index of the last element that passses the
condition