Arrays Flashcards

1
Q

How to create an array?

A

const arr = [‘1’ , ‘2’, ‘3’];

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

How to read a subset of an arrary?

A

use slice(start,end)

use splice if original array needs to be modified.

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

how to concat two array?

A

arr1.concat(arr2)

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

how to reverse an array?

A

arr1.reverse()

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

how to add a separator to all elements in an array?

A

arr1.join(‘*’);

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

When to use [] and at in array?

A

to get the last element and also for chaining…

arr1.[arr1.lenth -1] == arr1.at(-1)

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

How to loop through an array using forEach?

A

const arr = [‘1’ , ‘2’, ‘3’];

arr.forEach(function(mov,i,arrayOriginal)
{

});

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

How to use forEach for Maps?

A

map.forEach(function(value, key, originalMap)
{
});

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

How to use forEach for sets?

A

set.forEach(function(value, key, originalSet)
{
});

value and key will be same for sets as there is no key in sets. We can omit using _

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

What are different data transformation methods available for arrays?

A

There are 3 methods-
map - If we need to use multiplier
filter - to filter the results
reduce - to have a single value

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

How to use array map method?

A

—–ArrayMapmethod—–

constmapArr=[456,787,125,672,572,135];

constnewMapArr=mapArr.map(function(value,i,arrMap){
console.log(`valueis${value}`);
console.log(`iis${i}`);
returnvalue*1;
});

console.log(newMapArr);

—–Sameusingforof—–

for(constitemofmapArr){
console.log(samewithfor-of${item*1});
}

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

How to use filter method on arraya?

A

—–Arrayfiltermethod—–

constfilterArr=[456,787,125,672,572,135];

const newFilteredArr = filterArr.filter(function(mov,i,arr)
{
     return mov>100;
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to use reduce method for arrays?

A

—–Reducemethod—–

constreduceDemo=[430,1000,700,50,90];

constacc=reduceDemo.reduce(function(acc,value, i, originalArr){
returnacc+value;

},0);

console.log(acc);

Findthe largestnumber

constlargest=reduceDemo.reduce(function(acc,value){
if(acc

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

How to use find method?

A

Filter and find will search for an element. However, filter returns the array and find returns the single value (the first value).

—–Filter—-

constarrFilter=[29,45,23,78,54,53];

constfilterArr=arrFilter.filter(function(value,index,curr){
returnvalue

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

what is some and every method in arrays?

A

To remember use some and every CONDITION..

some returns true/false for some condition.

every returns true/false for every condition.

Includes method returns true/false if an element exists in the array.

But some method will return true/false if the condition is met. For every all elements should satisfy the condition.

arr.some(function(mov)
{
return mov>0;
}

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

How do you sort an array?

A

To sort an array, we need to use sort().

By default, it sorts based on the strings.

For numbers we need to provide the sort condition.

arr.sort(function(a,b)
{
if(a>b) return 1;
else return -1
});