# 陣列迭代

1. `forEach`
2. `map`
3. `filter`
4. `some`
5. `every`
6. `reduce`

## forEach

• 執行步驟：

1. 迭代整個陣列
2. 對每一個值（value）執行一次 callback
3. 回傳 `undefined`
• 運行機制：

1. Write a function called `doubleValues` which accepts an array and returns a new array with all the values in the array passed to the function doubled.

• Examples:

• Solution:

2. Write a function called `onlyEvenValues` which accepts an array and returns a new array with only the even values in the array passed to the function.

• Examples:

• Solution:

3. Write a function called `showFirstAndLast` which accepts an array of strings and returns a new array with only the first and last character of each string.

• Examples:

• Solution:

4. Write a function called `addKeyAndValue` which accepts an array of objects, a key, and a value and returns the array passed to the function with the new key and value added for each object.

• Examples:

• Solution:

5. Write a function called `vowelCount` which accepts a string and returns an object with the keys as the vowel and the values as the number of times the vowel appears in the string. This function should be case insensitive so a lowercase letter and uppercase letter should count.

• Examples:

• Solution:

## map

• 執行步驟：

1. 創建一個新的陣列（newArr）
2. 迭代原本的陣列（arr）
3. 對每一個值（value）執行一次 callback
4. 將 callback 函式回傳的結果加入步驟一所建立的陣列（newArr）
5. 回傳 newArr
• 運行機制：

1. Write a function called `doubleValues` which accepts an array and returns a new array with all the values in the array passed to the function doubled.

• Examples:

• Solution:

2. Write a function called `valTimesIndex` which accepts an array and returns a new array with each value multiplied by the index it is currently at in the array.

• Examples:

• Solution:

3. Write a function called `extractKey` which accepts an array of objects and some key and returns a new array with the value of that key in each object.

• Examples:

• Solution:

4. Write a function called `extractFullName` which accepts an array of objects and returns a new array with the value of the key with a name of “first” and the value of a key with the name of “last” in each object, concatenated together with a space.

• Examples:

• Solution:

## filter

• 執行步驟：

1. 創建一個新的陣列（newArr）
2. 迭代原本的陣列（arr）
3. 對每一個值（value）執行一次 callback

• 若 callback 回傳 `true`，將 value 加入步驟一所建立的陣列（newArr）
• 若 callback 回傳 `false`，continue
4. 回傳 newArr

• 運行機制：

1. Write a function called `filterByValue` which accepts an array of objects and a key and returns a new array with all the objects that contain that key.

• Examples:

• Solution:

2. Write a function called `find` which accepts an array and a value and returns the first element in the array that has the same value as the second parameter or undefined if the value is not found in the array.

• Examples:

• Solution:

3. Write a function called `findInObj` which accepts an array of objects, a key, and some value to search for and returns the first found value in the array.

• Examples:

• Solution:

4. Write a function called `removeVowels` which accepts a string and returns a new string with all of the vowels (both uppercased and lowercased) removed. Every character in the new string should be lowercased.

• Examples:

• Solution:

5. Write a function called `doubleOddNumbers` which accepts an array and returns a new array with all of the odd numbers doubled (HINT - you can use map and fitler to double and then filter the odd numbers).

• Examples:

• Solution:

## some

• 執行步驟：

1. 迭代原本的陣列（arr）
2. 對每一個值（value）執行一次 callback

• 若至少有一個 callback 回傳 `true`，則回傳 `true`
• 否則，回傳 `false`
• 運行機制：

## every

• 執行步驟：

1. 迭代原本的陣列（arr）
2. 對每一個值（value）執行一次 callback

• 若至少有一個 callback 回傳 `false`，則回傳 `fale`
• 否則，回傳 `true`
• 運行機制：

1. Write a function called `hasOddNumber` which accepts an array and returns `true` if the array contains at least one odd number, otherwise it returns `false`.

• Examples:

• Solution:

2. Write a function called `hasAZero` which accepts a number and returns `true` if that number contains at least one zero. Otherwise, the function should return `false`.

• Examples:

• Solution:

3. Write a function called `hasOnlyOddNumbers` which accepts an array and returns `true` if every single number in the array is odd. If any of the values in the array are not odd, the function should return `false`.

• Examples:

• Solution:

4. Write a function called `hasNoDuplicates` which accepts an array and returns `true` if there are no duplicate values (more than one element in the array that has the same value as another). If there are any duplicates, the function should return `false`.

• Examples:

• Solution:

5. Write a function called `hasCertainKey` which accepts an array of objects and a key, and returns `true` if every single object in the array contains that key. Otherwise it should return `false`.

• Examples:

• Solution:

6. Write a function called `hasCertainValue` which accepts an array of objects and a key, and a value, and returns `true` if every single object in the array contains that value for the specific key. Otherwise it should return `false`.

• Examples:

• Solution:

## reduce

• 執行步驟

• 參數：

1.（必要）callback 函式
2.（可有可無）第二個參數（optional parameter）

1. 迭代原本的陣列（arr）
2. 對每一個值（value）執行一次 callback

• 若有提供 optional parameter 的話，callback 的第一個參數便是該 optional parameter
• 否則，則是 arr[0]，也就是第 0 個 value
• 通常我們稱呼 callback 的第一個參數為 “accumulator”，常用縮寫為 acc
3. 每次從 callback 回傳的值，變成為新的 accumulator！

1. 無 optional parameter，最後回傳結果為 6。
accumulator nextValue returned value
1 2 3
3 3 6
1. 有 optional parameter，最後回傳結果為 16。
accumulator nextValue returned value
10 1 11
11 2 13
13 3 16

1. Write a function called `extractValue` which accepts an array of objects and a key and returns a new array with the value of each object at the key.

• Examples:

• Solution:

2. Write a function called `vowelCount` which accepts a string and returns an object with the keys as the vowel and the values as the number of times the vowel appears in the string. This function should be case insensitive so a lowercase letter and uppercase letter should count

• Examples:

• Solution:

3. Write a function called `addKeyAndValue` which accepts an array of objects, a key, and a value and returns the array passed to the function with the new key and value added for each object.

• Examples:

• Solution:

4. Write a function called `partition` which accepts an array and a callback and returns an array with two arrays inside of it. The partition function should run the callback function on each value in the array and if the result of the callback function at that specific value is true, the value should be placed in the first subarray. If the result of the callback function at that specific value is false, the value should be placed in the second subarray.

• Examples:

• Solution: