DevX JS - W6C15 - Array Methods.pptx
DevX JS - W6C15 - Array Methods.pptx
Array Methods
Review
Review
● Loops: ● Git GUI
○ While Loops ○ Source Tree
○ Do-While ○ Commit
○ Loop Keywords ○ Compare
● Programming ○ Stash
○ What is good code?
Arrays in JS
var arr = []
algorithmic problems.
arr.slice() // returns a subset of the array
arr.reverse() // flips the array
Arrays have MANY built-in methods.
arr.forEach() // does something to each item
Some problems cannot be easily solved by arr.splice() // adds items in a specific spot, removes
items
built-in methods. You should try to solve arr.sort() // orders the array
them by hand.
// THERE ARE MORE!!! THIS IS JUST SOME OF THEM!!!
Push and Pop
d var x = arr.pop();
a b c a b c
, , , , , ,
arr.push(“d”);
d
Unshift and Shift
! var x = arr.shift();
a b c b c
, , , , ,
arr.unshift("!");
a
Slice and Reverse
var x = arr.slice(1,3); var x = arr.reverse();
a b c d a b c d
, , , , , ,
b c d c b a
, , , ,
Array Methods
Array Methods
Solve each of these problems, starting with the array [1,2,3]. Console.log after each solution. Only
use array methods!
You pass a function parameter. The function takes an item from the array, and operates on it.
arr.forEach(logOdds); arr.forEach(
function logEvenOdd(val) { function (val) {
if (val % 2) { if (val % 2) {
console.log("ODD!"); console.log("ODD!");
} else { } else {
console.log("EVEN!"); console.log("EVEN!");
} }
} });
forEach( )
forEach( )
● Instruction: This one should be quick. Just console.log each item in the array.
arr.forEach(function (item) {
// your code here
});
map()
map(function(){})
Map is similar to forEach(). It will RETURN a new array, which is modified according to the rules
in your function argument provides.
aa bb cc
, ,
map()
var arr = ["Alex Bacon", "Zintis May", "John Wick",
1. x should not have first names. It var arr2 = [ "Course: Arrays", "Course: While
should have “Mr.” instead. Loops", "Course: If/Else", "Course: Promises" ];
Eg: “Mr. Bacon” var y = arr2.map(function (item) {
2. y should read “Lesson:” instead of var result;
“Course:”. // your code here
Eg: “Course: Arrays” return result;
});
console.log(y);
filter()
filter()
Filter is similar to map(). But the function receives an array-item and returns true or false. If it
return true, it keeps the item. Returning false removes the item from the results.
b c
,
filter()
var arr = ["Alex Bacon", "Zintis May", "John Wick",
1. x should contain no names with var arr2 = [ "Course: Arrays", "Course: While
the letter “a” in it! Loops", "Course: If/Else", "Course: Promises" ];
2. y should contain no lessons with y = arr2.filter(function (item) {
the letter “i” in it! var result;
// your code here
return result;
});
console.log(y);
reduce()
reduce()
This one is more complicated. Reduce takes two arguments. The point is to “reduce” the array
to a single value.
0
+item
6
Understanding reduce()
Try running this code. Check the console. Read it carefully to understand how it works.
function totalArrayValues(arr) {
var total = 0;
for (var item of arr) {
total += item;
}
return total;
}
splice()
splice(index, cut, insert)
splice() allows you to insert and remove sections of an array. It takes 2 or more arguments:
(1) Start of cut, (2) how many to cut, (3) what to insert.
You can also remove zero items, var arr2 = ["a", "d"];
and insert items. Like in the arr2.splice(1, 0, "b", "c");
second example.
console.log(arr2);
splice()
In this example, splice() removes items starting
at the ‘1’ index. It removes 2 items.
arr.splice(1,2);
arr now had ONLY ‘a’ and ‘d’.
a b c d
, , ,
a d
,
splice()
In this example, splice() removes items starting
at the ‘1’ index. It removes 2 items.
arr.splice(3,0,’x’,‘y’);
It also inserts “x” and “y” at the same spot.
a b c d x y
, , ,
a b c x y d
, , , , ,
splice()
var x = arr.splice(1,2); var x = arr.reverse();
a b c d a b c d
, , , , , ,
a d d c b a
, , , ,
Splice
// remove 1
Sorting var arr2 = [456, 12, 98, 876, 1, 23, 345, 100]
var arr3 = ['Alex Bacon', 'Zintis May', 'Charles
Messini', 'Devin Soriano']
Sort each of these arrays. Post
var arr4 = [ 'lightning', 'fort', 'to', 'cat',
the screen shot of the console
'store', 'strong', 'a', 'markets', 'phonetic' ]
when done!