-
Notifications
You must be signed in to change notification settings - Fork 0
/
spread-operator.js
76 lines (53 loc) · 2.02 KB
/
spread-operator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Spread operator
// copying and merging
const array = [1, 4, 7, 3, 5, 9]
const copiedArray = [...array]
console.log(copiedArray) //[ 1, 4, 7, 3, 5, 9 ]
console.log(...array) //1 4 7 3 5 9
const addingNumbers = [63, 85, 12]
// merging arrays
console.log(array.concat(addingNumbers)) // [
// 1, 4, 7, 3, 5,
// 9, 63, 85, 12
// ]
let mergedArray = [...array, ...addingNumbers][
// console.log(mergedArray)
// // [
// // 1, 4, 7, 3, 5,
// // 9, 63, 85, 12
// // ]
// console.log([7, ...mergedArray, 36])
(7, 1, 4, 7, 3, 5, 9, 63, 85, 12, 36)
]
// spread operator works on Iterables - arrays, strings, sets, maps --- not objects --> until the latest release
// const str = "Tricia"
// const letters = [...str]
// console.log(letters) // [ 'T', 'r', 'i', 'c', 'i', 'a' ]
// const myName = [...str, "Sykes"]
// console.log(myName) / ["T", "r", "i", "c", "i", "a", "Sykes"]
// console.log(`My name is ${[...letters]}`)
const order = (item1, item2, item3) => {
return `here is your order of ${item1}, ${item2}, and ${item3}`
}
const food = ["Cheeseburger", "Fries", "Beer"]
console.log(order(...food)) // here is your order of Cheeseburger, Fries, and Beer
// --------------- challenges from syllabus -----------------------
// Consider this function:
const combineArrays = (arrOne, arrTwo) => {
// return arrOne.concat(arrTwo) //oldschool
return [...arrOne, ...arrTwo]
}
console.log(combineArrays([2, 4, 2], [4, 6, 11])) //[ 2, 4, 2, 4, 6, 11 ]
// Refactor this function to use the spread operator to combine the arrays.
// Consider this function:
//
const combineAndFilterOdd = (arrOne, arrTwo, arrThree) => {
// return arrOne
// .concat(arrTwo)
// .concat(arrThree)
// .filter((num) => num % 2 !== 0)// old school
return [...arrOne, ...arrTwo, ...arrThree].filter((num) => num % 2 !== 0)
}
console.log(combineAndFilterOdd([3, 2, 5], [5, 8, 7], [4, 5, 6])) // [ 3, 5, 5, 7, 5 ]
// expected output: [ 3, 5, 5, 7, 5 ]
// Refactor this function to use the spread operator to combine the array arguments.