THE MAIN JAVASCRIPT ARRAY METHODS YOU NEED TO KNOW!
push()
This will add an element to the end of an array!
const array = ["hey", "hi", "hello"]
array.push("sup")
array now equals = ["hey", "hi", "hello", "sup"]
THREAD!

#100DaysOfCode #CodeNewbie #javascript
push()
This will add an element to the end of an array!
const array = ["hey", "hi", "hello"]
array.push("sup")
array now equals = ["hey", "hi", "hello", "sup"]
THREAD!


#100DaysOfCode #CodeNewbie #javascript
pop()
This will remove the last element of the array!
Super useful!
const array = ["hey", "hi", "hello"]
array.pop()
array now equals ["hey", "hi"]
very simple but useful!
This will remove the last element of the array!
Super useful!
const array = ["hey", "hi", "hello"]
array.pop()
array now equals ["hey", "hi"]
very simple but useful!
unshift()
This will place the element at the FRONT of the array!
const array = ["hey", "hi", "hello"]
array.unshift("sup")
array now equals ["sup","hey", "hi", "hello"]
This will place the element at the FRONT of the array!
const array = ["hey", "hi", "hello"]
array.unshift("sup")
array now equals ["sup","hey", "hi", "hello"]
shift()
This will REMOVE the element in the FRONT of the array!
const array = ["hey", "hi", "hello"]
array.shift()
array now equals ["hi", "hello"]
This will REMOVE the element in the FRONT of the array!
const array = ["hey", "hi", "hello"]
array.shift()
array now equals ["hi", "hello"]
splice()
This is a special one that can do a lot! VERY useful!
You can insert, delete and replace with this method!
.splice(position, items to remove)
This is an easy way to explain splice! But I will show multiple use cases so you can be a splice PRO!
This is a special one that can do a lot! VERY useful!
You can insert, delete and replace with this method!
.splice(position, items to remove)
This is an easy way to explain splice! But I will show multiple use cases so you can be a splice PRO!
const array = [ "hey", "hi", "hello"]
array.splice(1,1)
array now equals = ["hey", "hello"]
It removed the [1] element (we start counting at 0) so it took Hi!
const array = [ "hey", "hi", "hello"]
array.splice(2,1)
array now equals = ["hey", "hi"]
array.splice(1,1)
array now equals = ["hey", "hello"]
It removed the [1] element (we start counting at 0) so it took Hi!
const array = [ "hey", "hi", "hello"]
array.splice(2,1)
array now equals = ["hey", "hi"]
Now how about this?
const array = [ "hey", "hi", "hello"]
array.splice(1,0, "sup")
what do you think is happening here? Think about it for a second before continuing!
const array = [ "hey", "hi", "hello"]
array.splice(1,0, "sup")
what do you think is happening here? Think about it for a second before continuing!
the 0 means, we are deleting NOTHING. But we are going to now INSERT "sup" into this position!
so the array will NOW equal
array = ["hey", "sup, "hi", "hello"]
Straight

I know! Super useful!
so the array will NOW equal
array = ["hey", "sup, "hi", "hello"]
Straight



Want to replace something? We can use splice again!
const array = ["hey", "hi", "hello"]
array.splice(0,1, "sup")
array = ["sup", "hi", "hello"]
we went to the 0 element, deleted 1 element and inserted "sup"!
const array = ["hey", "hi", "hello"]
array.splice(0,1, "sup")
array = ["sup", "hi", "hello"]
we went to the 0 element, deleted 1 element and inserted "sup"!
Well, what if we want to keep the original array and make a NEW array, but with only some of the content?
slice() is the answer!
const array = ["hey, "hi", "hello"]
array.slice(0,1)
this would return ["hey"]
array.slice(0,2)
would return ["hey, "hi"]
slice() is the answer!
const array = ["hey, "hi", "hello"]
array.slice(0,1)
this would return ["hey"]
array.slice(0,2)
would return ["hey, "hi"]
join()
This will return your array AS A STRING and combine the elements! One of my favorite methods!
const array = ["hey, "hi", "hello"]
array.join()
this will return heyhihello
if we do array.join(" ") it will create a space after each element and return
hey hi hello
This will return your array AS A STRING and combine the elements! One of my favorite methods!
const array = ["hey, "hi", "hello"]
array.join()
this will return heyhihello
if we do array.join(" ") it will create a space after each element and return
hey hi hello
concat()
This will combine multiple arrays!
const array = ["hey, "hi", "hello"]
const array 2 = ["sup"]
const new = array.concat(array2)
this will give us ["hey", "hi", "hello", "sup"] the second array is added to the end.
This will combine multiple arrays!
const array = ["hey, "hi", "hello"]
const array 2 = ["sup"]
const new = array.concat(array2)
this will give us ["hey", "hi", "hello", "sup"] the second array is added to the end.
How about sorting values?
we can use sort()!
const numbers = [1, 8 ,99 ,3, 5]
function myFunction(){
numbers.sort(function(a, b){return a-b});
}
This sorting will return the numbers in ascending order
sort(function(a, b){return b-a})
would return the in descending!
we can use sort()!
const numbers = [1, 8 ,99 ,3, 5]
function myFunction(){
numbers.sort(function(a, b){return a-b});
}
This sorting will return the numbers in ascending order
sort(function(a, b){return b-a})
would return the in descending!
What if we just want it reversed?
then reverse() it!
const array = ["hey, "hi", "hello"]
array.reverse();
and it will return ["hello", "hi", "hey"]
then reverse() it!
const array = ["hey, "hi", "hello"]
array.reverse();
and it will return ["hello", "hi", "hey"]
Want to just return the array back as a string? Well that's easy!
toString() it!
const array = ["hey, "hi", "hello"]
array.reverse();
this returns hey, hi, hello
toString() it!
const array = ["hey, "hi", "hello"]
array.reverse();
this returns hey, hi, hello
what if we wanted to filter() our values?
const varietyOfAges = [18, 44, 16, 12, 60];
function checkIfOldEnough(ages) {
return age >= 18;
}
ages.filter(checkIfOldEnough);
This will filter all of the ages above or equal to 18!
const varietyOfAges = [18, 44, 16, 12, 60];
function checkIfOldEnough(ages) {
return age >= 18;
}
ages.filter(checkIfOldEnough);
This will filter all of the ages above or equal to 18!
Let me know if this thread helped you! Did I explain things in an easy enough way for you to grasp what I am talking about?
Hope this is a good reference and guide for you to use in the future!
Hope this is a good reference and guide for you to use in the future!
Forgot my most used method!!
map()
Mapping, in simple terms, is when we make a copy of an array & do something to it with a function!
const numbers = [1, 2, 3]
const new = http://numbers.map (myFunction)
function myFunction(num) {
return num * 10;
}
new = 10, 20, 30
map()
Mapping, in simple terms, is when we make a copy of an array & do something to it with a function!
const numbers = [1, 2, 3]
const new = http://numbers.map (myFunction)
function myFunction(num) {
return num * 10;
}
new = 10, 20, 30