JavaScript Backend Flashcards

1
Q

Read a JSON file

A

fetch(‘path/to/JSON.json’)
.then(res => res.json())
.then(data => {
console.log(data);
})
.catch(err => console.error(‘Error’: err));

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

Add to end of array

A

arr.push(4)

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

Remove starting element from array

A

arr.shift()

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

Remove last element in array

A

arr.pop()

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

Remove element/s in middle of array

A

arr.splice(1, 2)

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

Add element/s to middle of array

A

arr.splice(1, 0, 2, 3) //adds 2, 3 at index 1

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

Convert string to array

A

arr.split(‘’)

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

Extract substring

A

str.slice(0, 5)

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

Convert string to upper case

A

str.toUpperCase()

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

Convert string to lower case

A

str.toLowerCase

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

Does string include value

A

str.includes(‘a’)

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

Does a certain key exist in object

A

obj.hasOwnProperty(‘a’)
‘a’ in obj

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

Add key value pair to object

A

obj[‘c’] = 3

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

Get array of keys from object

A

Object.keys(obj)

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

Delete key value pair

A

delete obj.a

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

Loop through keys in an object

A

for (let key in obj) {
}

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

Initiate a set

A

mySet = new Set()
= new Set([1, 2, 3])

18
Q

Add value to set

A

mySet.add(4)

19
Q

How to see if value in set

A

mySet.has(3)

20
Q

Delete value from set

A

mySet.delete(2)

21
Q

Remove duplicates from an array

A

let newArr = [… new Set(array)]

22
Q

Find length of set

A

mySet.size()

23
Q

Iterate over values in a set

A

for (let value of mySet) {
}

24
Q

Clear set

A

mySet.clear()

25
Q

Create a map

A

let myMap = new Map()

26
Q

Add to a map

A

myMap.set(‘a’, 1)

27
Q

Get a value with key

A

myMap.get(‘a’)

28
Q

Does key exist in map

A

myMap.has(‘b’)

29
Q

Delete key value pair from map

A

myMap.delete(‘a’)

30
Q

Round down number

A

Math.floor(4.8) // 4

31
Q

Round up number

A

Math.ceil(4.2) // 5

32
Q

Find remainder of division

A

10 % 3 // 1

33
Q

Sort elements in place

A

arr.sort((a, b) => a -b)

34
Q

Pass filter on array

A

arr.filter(num => num > 2)

35
Q

Pass function on every element in array and return array

A

arr.map(num => num * 2)

36
Q

Execute function on each array element

A

arr.forEach(num => console.log(num))

37
Q

Write a normal function

A

function greet() {
console.log(‘Hello World’)
}

38
Q

Write an arrow function

A

let greet = () => {
console.log(‘Hello World’)
}

39
Q

Find character at index for string

A

str.charAt(7)

40
Q

Find index of substring in string

A

str.indexOf(‘World’)

41
Q

Replace substring with other string

A

str.replace(‘World’, ‘Universe’)