So, Arrays are a very important part of programming languages, they allow us to store multiple values in one variable, they can be muted and iterated through and they can be used in some really complex algorithms.
so, let's start off by Creating Arrays
// Create some Arrays
const numbers = [43,56,33,23,55,5];
const numbers2= new Array(22,45,33,85,65)
so, both of these are Arrays or both of these are the way to create an array. now, it is very obvious arrays don't have to be just numbers. Let's create an array of strings.
const fruit = ['Apple', 'Banana', 'Orange' , 'Pear']
const mixed= [22, "Hello", true, undefined, null, {a:1, b:1}, new Date()];
Now, first I just want to show you how to get the length in an array by length, I mean how many values are in the array and we can use this just as if we were to use it with a string.
//Get array length
const numbers = [43,56,30,20,44,35,50];
let val;
val = numbers.length;
console.log(val)
//output --> 7
If you will see the numbers array variable is having 7 values so it is showing the length of 7. We can also check to see if something's in the array. we do that with the array object. And then we can pass in whatever we want to test in this case the numbers array and if we save we get true.
const numbers = [43,56,30,20,44,35,50];
val = numbers.length;
//check if is array
val=Array.isArray(numbers)
//output --> true
This is a very simple process to check whether it is the array or not. And this is useful when we are dealing with the dom because we can return for instance a node list from the dom which is structured like an array but it's, not an actual array so we can't do certain things for example: for each loops, so it is a good way to test to see if something's in array and then you can convert it into an array if you need to
now to get a single value in array
const numbers = [43,56,33,23,55,5];
let val;
//Get a single value
val= numbers[3]
console.log(val)
//output--> 23
so, to get a single value from any array we will do val equals to numbers[3], exactly what we did that we want to use our brackets and we want to put in the position of the value that we want the index so here, in this case, let's say 3 and save it, so that gives us 23. All right and if we look up here 23 is actually the fourth value in the array, now the reason that this happens is because arrays are zero-based or one can say it starts from 0. so that's why we are getting 23 .
so, always remember that arrays are 0 based
Now, if we want to insert something into this array. arrays are not immutable, we can modify them
// Insert into array
const numbers = [43,56,33,23,36,5];
numbers[2]=100;
output:-> [43, 56, 100, 23, 44, 36, 5]
so, if we save that now and we look at the array that's being printed out. 100 is now in the 2nd index position.
so, now if we want to find the index value.
//Find the index of value
const numbers = [43,56,33,23,46,36,5];
val=numbers.indexOf(36)
and let's say we want to find the index for 36. And if we look up here 012345 so 36 is in the 5th position.
so now just look into mutating arrays with some different methods. so the first one is how to add on to the end of the array so we can do that with the method push.
//Mutating Arrays
numbers.push(250)
console.log(numbers)
//output--> [43, 56, 100, 23, 44, 36, 5, 250]
if you look up the array in the output we get 250 at the end.
so, what if we want to add on to the end as well as to the front
//Add on to the end
numbers.push(250)
//Add on to the front
numbers.unshift(120)
console.log(numbers)
//output:--> [120,43,56,100,23,44,36,5]
so we could see that 120 is the first value in the numbers array.
now, if we want to take off from the end we also can do that by using pop method.
//Take off from the end
let numbers=[120,43,56,100,23,44,56,5,5] ;
numbers.pop();
console.log(numbers);
//output:
[120,43,56,100,23,44,56,5]
so, if we want to take off numbers from the front, we can say numbers.shift instead of shift.
//Take off from front
let numbers=[120,43,56,100,23,44,56,5] ;
numbers.shift();
console.log(numbers)
//output:--> [43, 56, 100, 23, 44, 36, 5]
We can also splice out the values from any place in the array.Lets do that
let numbers=[43,56,100,23,44,56,5] ;
//Splice values
numbers.splice(1,1);
//output--> [43,100,23,44,56,5]
so, let's say we want to take out this 56 number, basically we are saying where we wanna start and where we wanna end in the splice function parameter.
Now, we can also reverse the whole array. Right now the numbers start from 43 and ends at 5.
let numbers=[43,56,100,23,44,56,5] ;
//Reverse
numbers.reverse():
//output:--> [5,56,44,23,100,56,43]
now, it starts from 5 and ends at 43.
All right so that was all about mutating Arrays
We can also Concatenate Arrays, so for that let's say Val equals and then we will take the numbers array and numbers2 array and we will do concat,
//Concatenate Array
let numbers=[1,2,5]
let numbers2= [9,5,4,8]
let val = numbers.concat(numbers2);
console.log(val)
//Output
[1,2,5,9,5,4,8]
Another thing we can do in the array is Sort
//Sorting Arrays
const fruit= ["Apple", "Banana", "Orange", "Pear"];
let val= fruit.sort();
console.log(val);
//Output:--> ["Apple", "Banana", "Orange", "Pear"]
The purpose of compare function is to get an alternative sort order. Depending on the basis of arguments passed the Compare function should return a negative, zero, or positive value. Compare function compares all values in the array, exactly two values at a time.
//Use the "Compare Function"
let numbers=[43,56,100,23,44,56,5] ;
let val= numbers.sort(function(x,y){
return x - y
});
console.log(val);
//output:--> [5, 23, 36, 43, 44, 56, 100]
Similar, if you want to find the reverse sort the array
//Reverse Sort Array
let val = numbers.sort(function(x,y){
return (y-x)
});
console.log(val);
//output:-->
[100, 56, 44, 43, 36, 23, 5]
Another method i want to mention is Find method, Find actually returns the first element provided in an array that satisfies the provided testing function.
//Find Method
const numbers= [43, 56, 33, 23, 44, 36, 5];
function under50(num){
return num < 50;
}
let val = numbers.find(under50);
console.log(val);
//output:--> 43
so, here 43 is the first one that's under 50.
another example if want to find the number over 50 in the numbers array
//Find
const numbers= [43, 56, 33, 23, 44, 36, 5];
function over50(num){
return num>50;
}
let val= numbers.find(over50);
console.log(val);
//output:--> 56
We could here easily figure out that 56 is the first one over 50.