Arrays in JavaScript — Part 2

📄 Table of Contents

Array Methods

◉ check if a variable is an array

variable.constructor === Array
Array.isArray(variable)

◉ Clone an Array:

arr.slice(0);

◉ get first element of an Array:

arr.slice(1);

◉ get last element of an Array:

arr.slice(-1);

◉ empty an array

array.length = 0;

◉ join all elements of an array:

//arr = ["A", "B", "C", "D"];
console.log(arr.toString()); // "A,B,C,D"
console.log(arr + ""); // "A,B,C,D"
console.log(arr.join()); // "A,B,C,D"
console.log(arr.join('+')); // A+B+C+D

Append an array to another array

var arr1 = [12 , "foo" , {name "Joe"} , -2458];
var arr2 = ["Doe" , 555 , 100];
Array.prototype.push.apply(arr1, arr2)/* arr1 will be equal to [12 , "foo" , {name "Joe"} , -2458 , "Doe" , 555 , 100] */

◉ Array find() Method

var ages = [3, 10, 18, 20];

function checkAdult(age) {
return age >= 18;
}

function myFunction() {
document.getElementById("demo").innerHTML = ages.find(checkAdult);
}

◉Array some() Method

◉Array.fill()

◉ Array.from()

◉Array.find()

Array.findIndex()

◉Array.of()

◉When to use forEach?

When to use map?

When to use filter?

let chars = ['A', 'B', 'A', 'C', 'B'];let uniqueChars = chars.filter((c, index) => {
return chars.indexOf(c) === index;
});
console.log(uniqueChars);
[ 'A', 'B', 'C' ]Code language: JSON / JSON with Comments (json)
let chars = ['A', 'B', 'A', 'C', 'B'];let dupChars = chars.filter((c, index) => {
return chars.indexOf(c) !== index;
});
console.log(dupChars);Code language: JavaScript (javascript)
[ 'A', 'B' ]

When to use find?

When to use reduce?

Quirks and Criticisms

const arr = [1,2,3];
const transformedArr = arr.map(function(){})
.filter(function(){});

A word about speed.

Experience with Front-end Technologies and MERN / MEAN Stack. Working on all Major UI Frameworks like React, Angular.