📂
LeetCode Note
  • Introduction
  • Tools
    • Clean Code
    • 英文小辭典
    • JS Reference
    • 常見 Edge Case
    • Array Method
    • Object Method
    • Function
    • Hashing
    • Prototype
    • 處理 Array 小撇步
    • String Method
    • Math / Date
    • loop
    • JSON.xx / localStorage
    • Date
    • Regex
    • Memorization
    • reduce condition
    • 命名
  • 筆記 Note
    • Promise
    • Walking the DOM
    • Element size and scrolling
    • CSS
  • Leetcode todo
    • ToDo
  • Array
    • # Select random poker without duplicates
    • # 最少替換達成不連續字串
    • # 724 Find Pivot Index
    • # 747. Largest Number At Least Twice of Others
    • # 01 getMaxProfit
    • # maxOfBiggestVal
    • # findSecondLargest
    • # 41 First Missing Positive
    • # 134 Gas Station (有圖)
    • # 202 Happy Number
    • # 344 Reverse String
    • # 412 Fizz Buzz
    • # 561 Array Partition I
    • # 804 Unique Morse Code Words
    • # 905 Sort Array By Parity
    • # 121. Best Time to Buy and Sell Stock.js
    • # 122 Best Time to Buy and Sell Stock II
    • # 189 Rotate Array
    • # 229 Majority Element II
    • # 268 Missing Number.
    • # 299 Bulls and Cows (有圖)
    • # 896 Monotonic Array
    • # 1002 Find Common Characters
    • # 1051 Height Checker
    • # 1185 Day of the Week
    • # 169 Majority Element
    • # 605. Can Place Flowers
    • # 350 Intersection of Two Arrays II (有圖)
    • # 482. License Key Formatting
  • Set / Map
    • # GetLengthOfLongestSubstring
    • #1 Two Sum
    • # 217 Contains Duplicate
    • # 1122 Relative Sort Array
    • # 1160 Find Words That Can Be Formed by Characters
    • #811 Subdomain Visit Count
    • # 349 Intersection of Two Arrays
    • # 819 Most Common Word
  • Two Pointer
    • #704. Binary Search
    • #26 Remove Duplicates from Sorted Array (有圖)
    • #27 Remove Element
    • # 66 Plus One
    • # 80 Remove Duplicates from Sorted Array II (有圖)
    • # 88 Merge Sorted Array (有圖)
    • # 125 Valid Palindrome
    • #167 Two Sum II - Input array is sorted (有圖)
    • # 283 Move Zeroes (有圖)
    • # 38 Count and Say
    • # 557. Reverse Words in a String III
    • #977 Squares of a Sorted Array
    • #209 Minimum Size Subarray Sum
  • String
    • # 13 Roman to Integer (有圖)
    • # 771 Jewels and Stones
    • # 937 Reorder Data in Log Files
    • # 929 Unique Email Addresses
    • # 1108 Defanging an IP Address
    • #14 Longest Common Prefix
    • # 387 First Unique Character in a String (有圖)
    • #193 Valid Phone Numbers
    • # 28 Implement strStr()
    • #383 Ransom Note
  • Stack
    • # 20 Valid Parentheses (有圖)
    • # 155 Min Stack
    • BF 165. remove characters
    • #1047 Remove All Adjacent Duplicates In String
  • Binary Search
    • # 1064 Fixed Point (有圖)
    • # 852 Peak Index in a Mountain Array
  • Recursion 遞迴
    • #2625. Flatten Deeply Nested Array
  • Math
    • # 7 Reverse Integer
    • # 9 Palindrome Number (有圖)
    • #53 Maximum Subarray (有圖)
    • # 1085 Sum of Digits in the Minimum Number.
    • # 136 Single Number
    • # 204 Count Primes (有圖)
    • #243 Shortest Word Distance
  • Dynamic Programing
    • # 322 Coin Change
    • # 509 Fibonacci Number (有圖)
    • # 70 Climbing Stairs
    • # 198 House Robber
    • # 168. Excel Sheet Column Title
  • Others
    • # 205. Isomorphic Strings
    • Implement js Array method
    • Flatten Array/Object
  • Matrix
    • 867. Transpose Matrix
  • Queue
    • DOM tree with queue
  • 排序
    • Different Sort
Powered by GitBook
On this page
  • Immutable way
  • 其他常用
  • Reimplement the Array method
  • Array.map
  • Array.filter

Was this helpful?

  1. Tools

Array Method

Previous常見 Edge CaseNextObject Method

Last updated 6 months ago

Was this helpful?

範例在此,

Method

原本陣列

return

Basic Array Method

push(ele)

Array 改變

Number (length)

unshift(ele)

Array 改變

Number (length)

pop()

Array 改變

被移掉的那個值

shift()

Array 改變

被移掉的那個值

concat()

不變

Array

Filter something

findIndex(callback)

不變

Number

find(callback)

不變

第一個找到的 "值"

filter(callback)

不變

符合條件的 Array

forEach(callback)

Array 改變

undefined

map(callback)

不變

數量為 array.length 的 Array

some(callback)

不變

Boolean

every(callback)

不變

Boolean

Special

reduce(acc, cur)

不變

值

Order

sort()

Array 改變

數量為 array.length 的 Array

toSorted()

不變

reverse()

Array 改變

數量為 array.length 的 Array

toReversed()

不變

Operation

slice(start, end)

不變

return 包含 start 不包含 end 的 Array

splice(start, deleteCount, insert)

Array 改變

回傳移掉的值 Array

toSpliced(start, deleteCount, insert)

不變

回傳結果Array

indexOf(ele)

不變

所在位置 Number

join()

不變

String

includes(ele)

不變

Boolean

flat([depth])

不變

const arr1 = [0, 1, 2, [3, 4]];

arr1.flat() // [0, 1, 2, 3, 4]

Immutable way

const [tasks, setTasks] = useState([
    { id: 0, content: "Walk the dog" },
    { id: 1, content: "Water the plants"},
    { id: 2, content: "Wash the dishes" },
]);
method
use

Add

concat

Delete

filter

其他常用

Method
判斷是否為 Array
Example

Array.isArray()

判斷是否為 Array

Array.isArray([]) // true Array.isArray("[]") // false

Array.from({ length: 5 })

Array.from({ length: 5 }). // [undefined, undefined...]. 總共五個

Array.from ({ length: 5 }, fn)

Array.from({length: 3}, () => Array(2).fill(0)) // [ [0,"0], [0,0], [0,0] ] Array.from({length: 3}, (_, i) => i + 1) // [1,2,3]

[ arr1, arr2 ] = [ arr2, arr1 ]

交換位置

let arr = [1,2,3]

[ arr[0], arr[2] ] = [ arr[2], arr[0] ] // [3,2,1]

arr[i] = arr[len -1] arr.pop()

把算過丟到最後然後 pop()

[1,2,3] 移掉 1

Object.fromEntries

把 array 變回 object

Object.fromEntries([[0,'a'], [1,'b']]) // [{0: 'a', 1: 'b'}

Reimplement the Array method

Array.map

/**
 * @param {number[]} arr
 * @param {Function} fn
 * @return {number[]}
 */
var map = function(arr, fn) {
    var result = [];
    for( let [index, value] of arr.entries()){
      result[index] = fn(value, index)
    }
    return result;
};

Array.filter

/**
 * @param {number[]} arr
 * @param {Function} fn
 * @return {number[]}
 */
var filter = function(arr, fn) {
    let result = [];
    let size = 0
    for(let [i, value] of arr.entries()) {
         if(fn(value, i)){
           result[size] = value
           size++;
         }
    }
    return result;
};
tasks.concat({ 
    id: Date.now().toString(), content: 'xx', 
})
[{ 
    id: Date.now().toString(), 
    content: value.trim() 
}, ...tasks]
tasks.filter((task, i) => i !== index)
it 邦