Wednesday Flashcards

(4 cards)

1
Q

spread operator

A

The spread operator expands or “spreads” an iterable (arrays, strings, objects) into separate arguments.

this ==> console.log (…‘hello’)
means
this ==> ‘h’, ‘e’, ‘l’, ‘l’, ‘o’ //by using spread you have a list of separate arguments

good reference article : https://medium.com/coding-at-dawn/how-to-use-the-spread-operator-in-javascript-b9e4a8b06fab

You can :
- COPY AN ARRAY OR OBJECT
const fruits = [‘🍏’,’🍊’,’🍌’,’🍉’,’🍍’]
const moreFruits = […fruits]; => could also be
const moreFruits = {…fruits} if fruits were an OBJECT with properties
console.log(moreFruits) // Array(5) [ “🍏”, “🍊”, “🍌”, “🍉”, “🍍” ]

REMEMBER : changes to the original array/object will NOT affect the copied array/object, which is what would happen if the array/object had been linked to the original with the assignment operator, =
const array = [‘😉’,’😊’,’😇’]
const copyWithEquals = array // Changes to array will change copyWithEquals
const copyWithSpread = […array] // Changes to array will not change copyWithSpread

array[0] = ‘😡’ // Whoops, a bug

console.log(…array) // 😡 😊 😇
console.log(…copyWithEquals) // 😡 😊 😇
console.log(…copyWithSpread) // 😉 😊 😇

COMBINE AN ARRAY OR OBJECT
const hello = {hello: “😋😛😜🤪😝”}
const world = {world: “🙂🙃😉😊😇🥰😍🤩!”}
const helloWorld = {…hello,…world}
console.log(helloWorld) // Object { hello: “😋😛😜🤪😝”, world: “🙂🙃😉😊😇🥰😍🤩!” }
const objectOne = {hello: “🤪”}
const objectTwo = {world: “🐻”}
const objectThree = {…objectOne, …objectTwo, laugh: “😂”} // can ADD or replace existing key value pairs in the new object as well
console.log(objectThree) // { hello: “🤪”, world: “🐻”, laugh: “😂” }

const myArray = [🤪,🐻,🎌]
const yourArray = [🙂,🤗,🤩]
const ourArray = […myArray,…yourArray]
console.log(…ourArray) // 🤪 🐻 🎌 🙂 🤗 🤩

const objectFour = {…objectOne, …objectTwo, laugh: () => {console.log(“😂”.repeat(5))}}
objectFour.laugh() // 😂😂😂😂😂

WITH THE MATH FUNCTION, TO CONVERT THE ARGUMENTS INTO LIST VALUES
const numbers = [37, -17, 7, 0]
console.log(Math.min(numbers)) // NaN
console.log(Math.min(…numbers)) // -17

const moreFruits = […fruits]; => could also be console.log(Math.max(…numbers)) // 37

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

describe how the slice and splice methods function, including parameters.

A

slice - returns a COPIED portion (or all) of an iterable.
parameters :
- slice (inclusiveStart, exclusiveEnd)
- slice (inclusiveStart) — goes to the end of iterable
- slice( ) —- makes a copy of the entire thing

splice - mutates original array, returns an array containing modified contents.
- splice (1, 2, 3)
1 — REQ’D inclusive index at which you want to start mutating array (if you add something, it will show up AT this index; if you delete something the element existing AT this index will be deleted)
2 — OPTIONAL how many things to delete
3 — items you’d like to insert (as many as you want separated by a comma)

https://www.udemy.com/course/the-web-developer-bootcamp/learn/lecture/21990046#overview

good link to discuss slice vs splice — https://www.freecodecamp.org/news/lets-clear-up-the-confusion-around-the-slice-splice-split-methods-in-javascript-8ba3266c29ae/

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

describe how the logical AND, OR and NOT operators function / evaluate data on both sides of itself.

A

&& —- returns the first falsey value or the last truthy value

|| —- returns the first truthy value
https://www.udemy.com/course/react-redux/learn/lecture/34695202#overview

! — gives the opposite of whatever it’s applied to

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

Make fake array (or two) in exercises and create practice examples.

array methods (description / return val) :
fill
flat
join
filter
two ways to combine arrays

A

fill -

flat - functionality / return : creates a new array with the sub-array elements concatenated into it up to the specified depth

join - concats elements in array into one string, separated by commas (default if no arg is passed in) or specified separator
syntax :
let letters = [ ‘T’, ‘C’, ‘E’, ‘P’, ‘S’, ‘E’, ‘R’ ];
let song = letters.reverse().join(‘.’);
//”R.E.S.P.E.C.T”

combine 2 arrays - firstArray.concat (secondArrayThatWillFollowTheFirst), spread operator

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