Powerful Array Methods
Filter, map, sort, and reduce.
Reference: Wes Bos: JS Array Cardio Practice #JavaScript30 4/30
Filter, map, sort, and reduce.
Reference: Wes Bos: JS Array Cardio Practice #JavaScript30 4/30
toString()
const names = ["Adam", "Ben", "Chris"]; let newString = names.toString(); // 'Adam,Ben,Chris' newString; // 'Adam,Ben,Chris' names; // (3) ['Adam', 'Ben', 'Chris']
join()
can.
names.toString(" - ");
// 'Adam,Ben,Chris'
// (Changing separator doesn't work, still comma separated)
toString()
is highly not recommended. Use join()
instead.toString()
for objects.
toString()
method.
const names = ["Adam", "Ben", "Chris"];
document.getElementById("exampleId").innerHTML = names.toString();
// is identical to:
document.getElementById("exampleId").innerHTML = names;
join(separator)
const names = ["Adam", "Ben", "Chris"]; let newString = names.join(" - "); // 'Adam - Ben - Chris' newString; // 'Adam - Ben - Chris' names; // (3) ['Adam', 'Ben', 'Chris']
pop()
pop()
method returns the popped element.const names = ["Adam", "Ben", "Chris"]; let popped = names.pop(); // undefined names; // (2) ['Adam', 'Ben'] popped; // 'Chris'
push(elements)
push()
method returns the new array length.const names = ["Adam", "Ben", "Chris"]; let pushed = names.push("Dodo"); // undefined names; // (4) ['Adam', 'Ben', 'Chris', 'Dodo'] pushed; // 4
const names = ["Adam", "Ben", "Chris"]; let pushed = names.push("Dodo", "Didi"); // undefined names; // (5) ['Adam', 'Ben', 'Chris', 'Dodo', 'Didi'] pushed; // 5
pop-push
vs shift-unshift
pop()
: pop out an element, push()
: push in an element.shift()
: shift out an element, unshift()
: unshift (back) in an element.pop-push
: Array's end.shift-unshift
: Array's beginning.pop-push
: Nothing, no position / index shiftingshift-unshift
: Their positions and indexes are all shifted, because removal/addition happens at index [0]. Because of this overall shifting effect, they called shift-unshift
.shift()
shift()
method returns the shifted out element.const names = ["Adam", "Ben", "Chris"]; let shifted = names.shift(); // undefined names; // (2) ['Ben', 'Chris'] shifted; // 'Adam'
unshift(elements)
unshift()
method returns the new array length.const names = ["Adam", "Ben", "Chris"]; let unshifted = names.unshift("Dodo", "Didi"); // undefined names; // (5) ['Dodo', 'Didi', 'Adam', 'Ben', 'Chris'] unshifted; // 5
[index]
const names = ["Adam", "Ben", "Chris"]; names[1] = "Dodo"; // 'Dodo' names; // (3) ['Adam', 'Dodo', 'Chris']
length
propertyconst names = ["Adam", "Ben", "Chris"]; names.length; // 3 names[names.length] = "Dodo"; // 'Dodo' names; // (4) ['Adam', 'Ben', 'Chris', 'Dodo'] names.length; // 4
delete
delete
only removes the content of an element, as addressed by the index number, without removing the spot itself. Leaving an undefined
hole.pop()
for removing the last elementshift()
for removing the first elementsplice(index, number)
for removing a certain number of elements at a certain position.const names = ["Adam", "Ben", "Chris"]; delete names[1]; // true names; // (3) ['Adam', empty, 'Chris'] names[1]; // undefined
concat()
const namesAB = ["Adam", "Ben"]; const namesCD = ["Chris", "David"]; const namesABCD = namesAB.concat(namesCD); namesABCD; // (4) ['Adam', 'Ben', 'Chris', 'David'] namesAB; // (2) ['Adam', 'Ben'] namesCD; // (2) ['Chris', 'David']
const namesAB = ["Adam", "Ben"]; const namesCD = ["Chris", "David"]; const namesEF = ["Ed", "Frank"]; const namesGH = ["Gerry", "Harry"]; const namesAtoH = namesAB.concat(namesCD, namesEF, namesGH); namesAtoH; // (8) ['Adam', 'Ben', 'Chris', 'David', 'Ed', 'Frank', 'Gerry', 'Harry']
const namesAC = ["Adam", "Ben", "Chris"]; const namesABCD = namesAC.concat("Dodo"); namesABCD; // (4) ['Adam', 'Ben', 'Chris', 'Dodo']
const namesAC = ["Adam", "Ben", "Chris"]; const namesABCD = namesAC.concat("Dodo", "Didi"); namesABCD; // (5) ['Adam', 'Ben', 'Chris', 'Dodo', 'Didi']
const namesAB = ["Adam", "Ben"]; const namesCD = ["Chris", "David"]; const namesEF = ["Ed", "Frank"]; const namesGH = ["Gerry", "Harry"]; const namesAtoH = namesAB.concat(namesCD, "Dodo", "Didi", namesEF, namesGH); namesAtoH; // (10) ['Adam', 'Ben', 'Chris', 'David', 'Dodo', 'Didi', 'Ed', 'Frank', 'Gerry', 'Harry']
splice()
const names = ["Adam", "Ben", "Chris", "Daniel", "Ed"]; let newNames = names.splice(1, 3, "Dodo", "Puffin"); // undefined newNames; // (3) ['Ben', 'Chris', 'Daniel'] names; // (4) ['Adam', 'Dodo', 'Puffin', 'Ed']
slice()
const names = ["Adam", "Ben", "Chris", "Daniel", "Ed"]; let slicedA = names.slice(2); // undefined let slicedB = names.slice(2, 4); // undefined slicedA; // (3) ['Chris', 'Daniel', 'Ed'] slicedB; // (2) ['Chris', 'Daniel'] names; // (5) ['Adam', 'Ben', 'Chris', 'Daniel', 'Ed']