101 Flashcards
Describe the .forEach() , method
forEach() is an array method
It calls a provided callbackFn function once for each element in an array in ascending-index order. Unlike map(), forEach() always returns undefined
forEach((element) => { /* … / })
forEach((element, index) => { / … / })
forEach((element, index, array) => { / … */ })
Describe the .map() , method
map() is an array method
that returns a new array with each element being the result of the callback function
Explain the following code:
const kvArray = [
{ key: 1, value: 10 },
{ key: 2, value: 20 },
{ key: 3, value: 30 },
];
const reformattedArray = kvArray.map(({ key, value }) => ({ [key]: value }));
console.log(reformattedArray); // [{ 1: 10 }, { 2: 20 }, { 3: 30 }]
console.log(kvArray);
// [
// { key: 1, value: 10 },
// { key: 2, value: 20 },
// { key: 3, value: 30 }
// ]
the map() function is taking in destructured { key, value } as an argument
const reformattedArray = kvArray.map(({ key, value }) => ({ [key]: value }));
What does .split() do ?
split() is a String method that returns an array of strings based on a separator argument
const str = ‘The quick brown fox jumps over the lazy dog.’;
const words = str.split(‘ ‘);
console.log(words[3]);
// Expected output: “fox”
const chars = str.split(‘’);
console.log(chars[8]);
// Expected output: “k”
const strCopy = str.split();
console.log(strCopy);
// Expected output: Array [“The quick brown fox jumps over the lazy dog.”]
What is logged?
const str = ‘The quick brown fox jumps over the lazy dog.’;
const words = str.split(‘ ‘); <= space
console.log(words);
[“The”, “quick”, “brown”, “fox”, “jumps”, “over”, “the”, “lazy”, “dog.”]
What is logged?
const str = ‘quickly’;
const chars = str.split(‘ ‘); <= empty string, no space
console.log(chars);
[“q”, “u”, “i”, “c”, “k”, “l”, “y”]
What does join() do?
join() is an array method that creates and returns a new string by concatenating all of the elements in an array, separated by commas or a specified separator string.
const elements = [‘Fire’, ‘Air’, ‘Water’];
console.log(elements.join());
// Expected output: “Fire,Air,Water” <= includes commas
console.log(elements.join(‘’)); <= empty string
// Expected output: “FireAirWater” <= no commas
console.log(elements.join(‘-‘));
// Expected output: “Fire-Air-Water”
What type of method is .join()
array method
what is the sort() method ?
array method
The sort() method sorts the elements of an array in place and returns the reference to the same array, now sorted
Rearrange the array into ascending order:
const array = [1, 30, 4, 21, 100000];
array.sort((a , b) => a - b);
console.log(array); <= mutates array!
// [1, 4, 21, 30, 100000]
Rearrange the array into descending order:
const array = [1, 30, 4, 21, 100000];
array.sort((a , b) => b - a);
console.log(array); <= mutates array!
// [100000, 30, 21, 4, 1]
const words = [‘spray’, ‘limit’, ‘elite’, ‘exuberant’, ‘destruction’, ‘present’];
How can you create a new list containing only words more than 6 characters long?
The filter() method creates a shallow copy of a portion of a given array, with elements that pass the test implemented by the provided function
const result = words.filter(word => word.length > 6);
console.log(result);
// Expected output: Array [“exuberant”, “destruction”, “present”]
what does the Array.includes() return ?
Boolean => true or false
the includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
includes(searchElement)
Example:
const pets = [‘cat’, ‘dog’, ‘bat’];
console.log(pets.includes(‘cat’));
// Expected output: true