Mozilla "Array" Flashcards
The ________ method creates a new, shallow-copied Array instance from an array-like or iterable object
Array.from()
console.log((\_\_\_\_\_\_\_\_\_\_'foo')); // expected output: Array ["f", "o", "o"]
console.log(\_\_\_\_\_\_\_\_\_\_\_([1, 2, 3], x => x * x)); // expected output: Array [2, 4, 6]
Array.from
The __________ method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
concat()
var array1 = ['a', 'b', 'c']; var array2 = ['d', 'e', 'f'];
console.log(array1.\_\_\_\_\_\_\_\_(array2)); // expected output: Array ["a", "b", "c", "d", "e", "f"]
concat
var array1 = ['a', 'b', 'c']; var array2 = ['d', 'e', 'f'];
console.log(\_\_\_\_\_\_\_\_\_\_\_\_\_); // expected output: Array ["a", "b", "c", "d", "e", "f"]
array1.concat(array2)
The_______method creates a new Array instance from a variable number of arguments, regardless of number or type of the arguments.
Array.of()
The ___________ method determines whether the passed value is an Array.
Array.isArray()
Array.of(7); //
[7]
Array(7) //
[ , , , , , , ]
console.log(Array.\_\_\_\_\_([1, 2, 3], x => x + x)); // expected output: Array [2, 4, 6]
from
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. If the array has only one item, then that item will be returned without using the separator.
join()
var elements = [‘Fire’, ‘Air’, ‘Water’];
console.log(elements.join()); // expected output:
console.log(elements.join('-')); // expected output:
“Fire,Air,Water”
“Fire-Air-Water”
var elements = [‘Fire’, ‘Air’, ‘Water’];
console.log(\_\_\_\_\_\_\_\_\_\_\_\_); // expected output: "Fire-Air-Water"
elements.join(‘-‘)
The ________ method returns a new Array Iterator object that contains the keys for each index in the array.
keys()
The __________ method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.
reverse()
The _______ method creates and returns a new string, the ______ method a array
join()
concat()
The ____________ method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
every()
The every() returns a _________
Boolean value.
function isBelowThreshold(currentValue) { return currentValue < 40; }
var array1 = [1, 30, 39, 29, 10, 13];
console.log(\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_); // expected output: true
array1.every(isBelowThreshold)
Array.of(7); //
Array.of(1, 2, 3); //
[7]
[1, 2, 3]
The ________ method sorts the elements of an array in place and returns the sorted array. The default sort order is built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.
sort()
The _________ method returns a string representing the specified array and its elements.
toString()
var array1 = [1, 2, ‘a’, ‘1a’];
console.log(\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_); // expected output: "1,2,a,1a"
array1. toString()
array1. join()
The __________ method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
includes()
includes() returns _________?
true or false
2 ways of creating array
let arr =
let arr =
new Array(); [];
let fruits = ["Apple", "Orange", "Plum"]; fruits[2] = 'Pear'; //
now [“Apple”, “Orange”, “Pear”]
let fruits = ["Apple", "Orange", "Plum"]; fruits[3] = 'Lemon';
// now [“Apple”, “Orange”, “Pear”, “Lemon”]
let fruits = [“Banana”]
let arr = fruits;
alert( arr === fruits ); //
true
let fruits = [“Apple”, “Orange”, “Plum”];
// iterates over array elements USING FOR OF
for (let fruit of fruits) {
alert( fruit );
}
let arr = [1, 2, 3, 4, 5];
arr.length = 2;
alert( arr ); // [1, 2]
arr.length = 5; // return length back
alert( arr[3] ); // ??
undefined: the values do not return
simplest way to clear the array is _______
arr.length = 0;
let arr = new Array(2)
alert( arr[0] ); //
undefined! no elements.
let arr = new Array(2); // will it create an array of [2] ?
alert( arr.length ); //
length 2
let arr = [“I”, “go”, “home”];
delete arr[1];
alert( arr[1] ); //
alert( arr.length ); //
undefined
3
The _________ method allows to run a function for every element of the array.
forEach
arr.forEach(function(\_\_\_\_ , \_\_\_\_\_\_\_\_ , \_\_\_\_\_\_\_) { // ... do something with item });
item, index, array
3 search methods for array
indexOf,
lastIndexOf
includes
let arr = [1, 0, false];
alert( arr.indexOf(0) ); //
alert( arr.indexOf(false) ); //
alert( arr.indexOf(null) ); //
alert( arr.includes(1) ); //
1
2
-1
true
let result = arr.find(function(__ , ______ , ______) {
});
returns
item, index, array
let users = [ {id: 1, name: "John"}, {id: 2, name: "Pete"}, {id: 3, name: "Mary"} ];
let user = users.find(item => item.id == 2);
alert(user.name); //
Pete
let users = [ {id: 1, name: "John"}, {id: 2, name: "Pete"}, {id: 3, name: "Mary"} ];
let user = users.find(_____________);
alert(user.name); // John
item => item.id == 1
var myA1 = [1,2,3]; var myA2 = [1,2,3];
console.log(myA1==myA2);
false
let arr = [ 1, 2, 15 ];
// the method reorders the content of arr (and returns it) arr.sort();
alert( arr ); //
1, 15, 2
let arr = [ 1, 2, 15, 155 ];
arr.sort();
alert( arr );
1, 15, 155, 2
“2” > “15”
TRUE
function compareNumeric(a, b) { if (a > b) return if (a == b) return if (a < b) return }
1
0
-1
let str = “test”;
alert(________); // t,e,s,t
str.split(‘’)
When we need to iterate over an array – we can use _____________________ name all 3
forEach, for or for..of.
The ____________ method executes a provided function once for each array element.
forEach()
_________— executes a provided function once for each array element.
__________— creates a new array with the results of calling a provided function on every element in the calling array.
forEach()
map()
The ________method shallow copies part of an array to another location in the same array and returns it without modifying its length.
copyWithin()
copyWithin()
The copyWithin() method shallow copies part of an array to another location in the same array and returns it without modifying its length.
arr.copyWithin(target[, start[, end]])
var array1 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’];
console.log(array1.copyWithin(0, 3, 4)); // expected output:
Array [“d”, “b”, “c”, “d”, “e”]
var array1 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’];
console.log(array1.copyWithin(1, 3)); // expected output:
Array [“d”, “d”, “e”, “d”, “e”]
arr.copyWithin(________)
target[, start[, end]]
The ___________ method returns the first index at which a given element can be found in the array, or -1 if it is not present.
indexOf()
var beasts = [‘ant’, ‘bison’, ‘camel’, ‘duck’, ‘bison’];
console.log(beasts.indexOf('bison')); // expected output:
1
var beasts = [‘ant’, ‘bison’, ‘camel’, ‘duck’, ‘bison’];
// start from index 2 console.log(beasts.indexOf('bison', 2)); // expected output:
4
var beasts = [‘ant’, ‘bison’, ‘camel’, ‘duck’, ‘bison’];
console.log(beasts.indexOf('giraffe')); // expected output:
// expected output: -1
arr.indexOf(_________[,__________])
searchElement
fromIndex
var array1 = [1, 2, 3];
console.log(array1); // expected output:
Array [1, 2, 3]
arr.copyWithin(______[,________[, ______]])
target . start . end
USE .some or .every?
const userPrivileges = ['user', 'user', 'user', 'admin']; const containsAdmin = userPrivileges.\_\_\_\_\_\_\_( element => element === 'admin'); // containsAdmin will be equal to true
some
use .some or .every?
const ratings = [3, 5, 4, 3, 5]; const goodOverallRating = ratings.\_\_\_\_\_\_( rating => rating >= 3 ); // goodOverallRating will be equal to true
every
const icecreamColors = { chocolate: 'brown', vanilla: 'white', strawberry: 'red', }
const colors =\_\_\_\_\_\_\_\_\_\_; // colors will be equal to ["brown", "white", "red"]
Object.values(icecreamColors)
const icecreamColors = { chocolate: 'brown', vanilla: 'white', strawberry: 'red', }
const types = \_\_\_\_\_\_\_\_\_ // types will be equal to ["chocolate", "vanilla", "strawberry"]
Object.keys(icecreamColors);
var arr = [[1,2], [3,4], [5,6]]; alert the number 5
alert(arr[2][0]);
The _________ method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.
some()
The _________ method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.
lastIndexOf()
var animals = ['Dodo', 'Tiger', 'Penguin']; console.log(animals.lastIndexOf('Penguin')); // expected output:
console.log(animals.lastIndexOf('Dodo')); // expected output:
2
0
The___________ method fills (modifies) all the elements of an array from a start index (default zero) to an end index (default array length) with a static value. It returns the modified array.
fill()
var array1 = [1, 2, 3, 4];
array1.fill(6) returns
// expected output: [6, 6, 6, 6]
var array1 = [1, 2, 3, 4];
console.log(array1.fill(0, 2, 4));
// expected output: [1, 2, 0, 0]
// fill with 0 from position 2 until position 4
var array1 = [1, 2, 3, 4]; console.log(array1.fill(5, 1));
// expected output: [1, 5, 5, 5]
var array = [2, 9, 9];
array.indexOf(7); //
-1
var array = [2, 9, 9];
array.indexOf(9, 2); //
2
var array = [2, 9, 9]; array.indexOf(2, -1); //
-1
var array = [2, 9, 9]; array.indexOf(2, -3); //
0
__________syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
Spread operator
function sum(x, y, z) { return x + y + z; }
const numbers = [1, 2, 3];
console.log(sum(...numbers)); // expected output:
6
function sum(x, y, z) { return x + y + z; }
const numbers = [1, 2, 3];
console.log(sum.apply(null, numbers)); // expected output:
6
Concating these two arrays will result//
my arr1 = [[“steve”, 33],[“me”, 22]]
my arr2 = [“john”, 55]
[[“steve”, 33],[“me”, 22], “john”, 55]
Concating these two arrays will result//
my arr1 = [[“steve”, 33],[“me”, 22]]
my arr2 = [[“john”, 55]]
[[“steve”, 33],[“me”, 22], [“john”, 55]]
let array1 = [1,2,3];
let array2 = [4,5,6];
let array3 = // concat using … spread syntax
[…array1,…array2];
let array1 = [1,2,3];
let array2 = [4,5,6];
let array3 = // concat using concat()
console.log(array1.concat(array2));
var blue = { color:blue; }
let colors = [{color:red}, {color:blue}];
var index = indexOf(blue) // returns
-1
referencing the blue object
var blue = { color:blue; }
let colors = [{color:red}, blue];
var index = indexOf(blue) // returns
1
referencing from array
arr.indexOf(_________)
searchElement[, fromIndex]