Array Methods Flashcards
let arr = [“I”, “go”, “home”];
delete arr[1];
alert( arr[1] ); // returns?
undefined
That’s natural, because delete obj.key removes a value by the key. It’s all it does. Fine for objects. But for arrays we usually want the rest of elements to shift and occupy the freed place. We expect to have a shorter array now.
let arr = [“I”, “go”, “home”];
delete arr[1];
alert( arr.length ); // returns?
3
now arr = [“I”, , “home”];
let arr = [“I”, “study”, “JavaScript”];
arr.splice(1, 1);
alert( arr ); // returns?
[“I”, “JavaScript”]
let arr = [“I”, “study”, “JavaScript”, “right”, “now”];
arr.splice(0, 3, “Let’s”, “dance”);
alert( arr ) // returns?
[“Let’s”, “dance”, “right”, “now”]
let arr = [“I”, “study”, “JavaScript”, “right”, “now”];
let removed = arr.splice(0, 2);
alert( removed ); // returns?
“I”, “study” <– array of removed elements
let arr = [“I”, “study”, “JavaScript”];
arr.splice(2, 0, “complex”, “language”);
alert( arr ); // returns?
“I”, “study”, “complex”, “language”, “JavaScript”
The method ________creates a new array that includes values from other arrays and additional items.
arr.concat
The _______ method allows to run a function for every element of the array.
arr.forEach
_______– looks for item starting from index from, and returns the index where it was found, otherwise -1.
arr.indexOf(item, from)
________item, from) – looks for item starting from index from, returns true if found.
arr.includes()
let arr = [1, 0, false];
alert( arr.indexOf(0) ); //
alert( arr.indexOf(null) ); //
1
-1
The method _______ is the same as indexOf, but looks for from right to left.
arr.lastIndexOf
Imagine we have an array of objects. How do we find an object with the specific condition?
arr.find(fn)
let result = arr.find(function(item, index, array) {
// if true is returned, item is returned and iteration is stopped
// for falsy scenario returns undefined
});
et users = [
{id: 1, name: “John”},
{id: 2, name: “Pete”},
{id: 3, name: “Mary”}
];
let user = users.find( );
write code
alert(user.name); // John
item => item.id == 1
const arr = [NaN];
alert( arr.indexOf(NaN) ); //
alert( arr.includes(NaN) );//
-1
true (correct)
The_____method creates a new array populated with the results of calling a provided function on every element in the calling array.
map()
let lengths = [“Bilbo”, “Gandalf”, “Nazgul”].map(item => item.length);
alert(lengths); //
5,7,6
let lengths = [“Bilbo”, “Gandalf”, “Nazgul”]
using map return results
alert(lengths); // 5,7,6
.map(item => item.length);
The _____method sorts the elements of an array in place and returns the reference to the same array, now sorted.
sort()
let arr = [ 1, 2, 15 ];
arr.sort();
alert( arr ); //
1, 15, 2
in arrays all elements are converted to ______ for comparisons.
strings
let arr = [1, 2];
alert( arr.concat([3, 4], 5, 6) ); //
1,2,3,4,5,6
let arr = [1, 2];
let arrayLike = {
0: “something”,
length: 1
};
alert( arr.concat(arrayLike) ); //
1,2,[object Object]
The _____method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function
filter()
let arr = [ 1, 2, 15 ];
arr.sort
// write the sort function
alert(arr); // 1, 2, 15
(function(a, b) { return a - b; });
arr.sort(function(a, b) { return a - b; });
WRITE AS AN ARROW
arr.sort( (a,b) => a - b );
in arrays For many alphabets, it’s better to use _______ method to correctly sort letters
str.localeCompare
The method ________reverses the order of elements in arr.
arr.reverse
The _______method takes a pattern and divides a String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array
split()
let str = “test”;
alert( str.split(‘’) ); //
t,e,s,t
The ______ method takes a pattern and divides a String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.
split()
The_______ method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string.
join()
let arr = [‘Bilbo’, ‘Gandalf’, ‘Nazgul’];
let str =
// glue the array into a string using ;
alert( str ); // Bilbo;Gandalf;Nazgul
arr.join(‘;’);
let arr = [‘Bilbo’, ‘Gandalf’, ‘Nazgul’];
let str = arr.join(‘;’); // glue the array into a string using ;
alert( str ); //
Bilbo;Gandalf;Nazgul
When we need to iterate and return the data for each element – we can use
map.
When we need to iterate over an array – we can use name all 3
for
for…of
for Each
let arr = [1, 2, 3, 4, 5];
let result = arr.reduce((sum, current) => sum + current, 0);
alert(result); // returns and why
15
let arr = [1, 2, 3, 4, 5];
// removed initial value from reduce (no 0)
let result = arr.reduce((sum, current) => sum - current);
alert( result ); //
-13
let arr = [1, 2, 3, 4, 5];
let result = arr.reduce((sum, current) => sum + current, 4);
alert(result); //
19
alert(typeof {}); //
object
alert(typeof []); //
object
alert(Array.isArray({})); //true or false
false
alert(Array.isArray([])); // true or false
true