ARRAYS. Flashcards

1
Q

What is an Array?

A

1.) A type of variable used in many computer programs to store data.

2.) An array represents a list of elements, each storing a specific value and associated with a number called its index. The first element of an array will be index number 0 - not 1.

3.)

4.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you Iterate an Array?

A

To iterate over an array (browsing it element by element), you can use the for loop, the forEach() method or the newer for-of loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Push() Method

A

The push() method adds an element at the end of an array. The unshift() method adds it at the beginning.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Pop( ) and shift( )

A

Are used to remove elements from the array.
The key difference between pop() and shift() and their cousins push() and unshift(), is that neither method takes parameters, and each only allows an array to be modified by a single element at a time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you create an Array?

A

An array is created with a pair of square brackets [ ]. Everything within the brackets makes up the array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does Splice( ) work?

A

1.) The arr.splice method is a Swiss army knife for arrays.
It can do everything:
insert, remove and replace elements.

arr.splice(start[, deleteCount, elem1, …, elemN])

2.) The splice method is also able to insert the elements without any removals. For that we need to set deleteCount to 0:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does Slice ( ) work?

A

1.) It returns a new array copying to it all items from index start to end (not including end)

2.) Both start and end can be negative, in that case position from array end is assumed.

3.) It’s similar to a string method str.slice, but instead of sub-strings it makes sub-arrays.

let arr = [“t”, “e”, “s”, “t”];

alert( arr.slice(1, 3) ); // e,s (copy from 1 to 3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does Concat ( ) work?

A

1.) The method arr.concat creates a new array that includes values from other arrays and additional items.

The syntax is:

arr.concat(arg1, arg2…)

2.) It accepts any number of arguments – either arrays or values.

3.) The result is a new array containing items from arr, then arg1, arg2 etc.

4.) If an argument argN is an array, then all its elements are copied. Otherwise, the argument itself is copied.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

forEach ( )

A

arr.forEach(function(item, index, array) {
// … do something with item
});

1.) The arr.forEach method allows to run a function for every element of the array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Searching in an Array Methods

A

1.) The methods arr.indexOf and arr.includes have the similar syntax and do essentially the same as their string counterparts, but operate on items instead of characters:

2.) arr.indexOf(item, from) – looks for item starting from index from, and returns the index where it was found, otherwise -1.

3.) arr.includes(item, from) – looks for item starting from index from, returns true if found.

4.) Usually these methods are used with only one argument: the item to search. By default, the search is from the beginning.

5.) If we want to check if item exists in the array, and don’t need the exact index, then arr.includes is preferred.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Find ( )

A

let result = arr.find(function(item, index, array) {
// if true is returned, item is returned and iteration is stopped
// for falsy scenario returns undefined
});

1.) In real life arrays of objects is a common thing, so the find method is very useful.

2.) Note that in the example we provide to find the function item => item.id == 1 with one argument. That’s typical, other arguments of this function are rarely used.

3.) The arr.findIndex method has the same syntax, but returns the index where the element was found instead of the element itself. The value of -1 is returned if nothing is found.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

map() method

A

The map() method is an iterative and copying array method. This means, it creates a new array from an already existing array. It is able to do this by using a callback function which mutate the original array. The new array will have the same length as the original array, but with the elements modified by the provided function. The callback function used in the syntax can be arrow function or regular inline function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a Spread Operator?

A

ES6’s new spread operator allows us to easily copy all of an array’s elements, in order, with a simple and highly readable syntax. The spread syntax simply looks like this: …

How well did you know this?
1
Not at all
2
3
4
5
Perfectly