寫在前面 tips:點贊 + 收藏 = 學會! 本文包含radash中數組相關的所有方法說明 + 使用示例 + 思維導圖查看 這邊會整理出一份數組相關方法的使用大綱(不含源碼解析),方便大家查閱使用; 作者會按照大類進行整理分享,本次也會同步給出Array所有方法的思維導圖; 所有方法整理完畢後,作 ...
寫在前面
tips:點贊 + 收藏 = 學會!
- 本文包含radash中數組相關的所有方法說明 + 使用示例 + 思維導圖查看
- 這邊會整理出一份數組相關方法的使用大綱(不含源碼解析),方便大家查閱使用;
- 作者會按照大類進行整理分享,本次也會同步給出
Array
所有方法的思維導圖; - 所有方法整理完畢後,作者會整理出一份Radash庫相關的所有方法的思維導圖,方便大家查看使用。
- 作者會按照大類進行整理分享,本次也會同步給出
- 本文會把每個方法做標題展示,也方便大家直接在本文中閱讀對應方法內容。
數組方法彙總思維導圖
alphabetical:把對象數組按照選定key的value的字母順序排列
- 用法說明
- 參數:目標對象數組、用於排序的屬性的回調函數、第三個參數可選(不傳是升序排序,傳入
desc
字元則表示降序排序); - 返回值:排序後的數組。
- 參數:目標對象數組、用於排序的屬性的回調函數、第三個參數可選(不傳是升序排序,傳入
- 使用代碼示例:
import { alphabetical } from 'radash' const ig = [ { name: 'ning', power: 100 }, { name: 'rookie', power: 98 }, { name: 'jkl', power: 95 }, { name: 'theshy', power: 100 } ] // 這裡輸出的依然是對象數組,這裡簡單表示 alphabetical(ig, g => g.name) // => [jkl, ning, rookie, theshy] alphabetical(ig, g => g.name, 'desc') // => [theshy, rookie, ning, jkl]
boil:找出對象數組中滿足條件的對象
- 用法說明:
- 參數:目標對象數組、條件函數;
- 返回值:滿足條件的對象。
- 使用代碼示例:
import { boil } from 'radash' const rng = [ { name: 'Uzi', power: 100 }, { name: 'Xiaohu', power: 98 }, { name: 'Ming', power: 72 } ] boil(gods, (a, b) => (a.power > b.power ? a : b)) // => { name: 'Uzi', power: 100 } boil(gods, (a, b) => (a.power < b.power ? a : b)) // => { name: 'Ming', power: 72 }
cluster:把一個數組儘量均勻的分成多個數組
- 用法說明:
- 參數:目標數組、分組個數n;
- 返回值:分組後的二維數組。
- 使用代碼示例:
import { cluster } from 'radash' const gods = ['Ra', 'Zeus', 'Loki', 'Vishnu', 'Icarus', 'Osiris', 'Thor', 'Apollo', 'Artemis', 'Athena'] cluster(gods, 3) // => [ // [ 'Ra', 'Zeus', 'Loki' ], // [ 'Vishnu', 'Icarus', 'Osiris' ], // ['Thor', 'Apollo', 'Artemis'], // ['Athena'] // ]
counting:統計對象數組中每個唯一標識符的出現次數
- 用法說明:
- 參數:目標對象數組、條件函數(內部是傳入目標對象,返回對象身上的某一項——根據這項來做統計);
- 返回值:統計對象。
- 使用代碼示例:
import { counting } from 'radash' const skt = [ { name: 'Ra', culture: 'egypt' }, { name: 'Zeus', culture: 'greek' }, { name: 'Loki', culture: 'greek' } ] counting(gods, g => g.culture) // => { egypt: 1, greek: 2 }
diff:返回數組 1 中出現但是沒在數組 2 中出現的項
- 用法說明:
- 參數:目標數組1、目標數組2;
- 返回值:包含符合項的數組。
- 使用代碼示例:
import { diff } from 'radash' const oldWorldGods = ['rng', 'uzi'] const newWorldGods = ['vishnu', 'uzi'] diff(oldWorldGods, newWorldGods) // => ['rng']
first:獲取數組第一項,不存在返回預設值
- 使用說明
- 參數:目標數組,或者傳遞兩個參數空數組和預設值;
- 返回值:傳目標數組則返回目標數組的第一項,傳空數組和預設值則返回預設值。
- 使用代碼示例
import { first } from 'radash' const gods = ['lufee', 'loki', 'zeus'] first(gods) // => 'lufee' first([], 'zuoluo') // => 'zuoluo'
flat:數組扁平化 —— 把包含多個數組的數組轉為一個數組(註意不會遞歸)
-
使用說明
- 參數:包含多個數組的數組;
- 返回值:降低一維後的數組;
- 註意:不會遞歸降維,只能降一維。
-
使用代碼示例
import { flat } from 'radash' const gods = [['shy', 'ning'], ['jkl']] flat(gods) // => [shy, ning, jkl]
fork:按條件將數組拆分成兩個數組,滿足條件的一個,不滿足條件的一個
- 使用說明
- 參數:目標數組,條件函數;
- 返回值:返回兩個數組,一個保存滿足條件的項,另一個保存不滿足條件的項。
- 使用代碼示例
import { fork } from 'radash' const gods = [ { name: 'Uzi', power: 100 }, { name: 'Xiaohu', power: 98 }, { name: 'Ming', power: 72 }, { name: 'Mlxg', power: 100 } ] const [finalGods, lesserGods] = fork(gods, f => f.power > 90) // [[Uzi, Xiaohu, Mlxg], [Ming]]
group:根據條件函數指定的key構建一個統計對象,key為指定的key有哪些value,value為對應對象
- 使用說明
- 參數:對象數組、條件函數;
- 返回值:統計對象
- 使用代碼示例
import { group } from 'radash' const fish = [ { name: 'Marlin', source: 'ocean' }, { name: 'Bass', source: 'lake' }, { name: 'Trout', source: 'lake' } ] const fishBySource = group(fish, f => f.source) // => { ocean: [marlin], lake: [bass, trout] }
intersects:判斷兩個數組是否有公共項,返回一個布爾值
- 使用說明
- 參數:數組1,數組2,可選條件函數(用於提取隨機標識符,對對象數組進行操作時);
- 返回值:有返回true,否則返回false。
- 使用代碼示例
import { intersects } from 'radash' const oceanFish = ['tuna', 'tarpon'] const lakeFish = ['bass', 'trout'] intersects(oceanFish, lakeFish) // => false const brackishFish = ['tarpon', 'snook'] intersects(oceanFish, brackishFish) // => true
iterate:把一個函數迭代執行指定次數
- 使用說明
- 參數:迭代次數、每次迭代調用的函數、迭代初始值。
- 返回值:返回最終一次迴圈迭代的值。
- 使用代碼示例
import { iterate } from 'radash' const value = iterate( 4, (acc, idx) => { return acc + idx }, 0 ) // => 10
last:輸出數組的最後一項,如果數組為空則輸出傳入的預設值
- 使用說明
- 參數:目標數組,或者空數組和預設值。
- 返回值:數組最後一項,如果數組為空則輸出傳入的預設值。
- 使用代碼示例
import { last } from 'radash' const fish = ['marlin', 'bass', 'trout'] const lastFish = last(fish) // => 'trout' const lastItem = last([], 'bass') // => 'bass'
list:創建包含特定項的數組
- 使用說明
- 參數:start、end、值,步長。
- 返回值:從start開始遍歷到end,輸出一個數組,包含特定項(值)的數組。
- 使用代碼示例
import { list } from 'radash' list(3) // [0, 1, 2, 3] list(0, 3) // [0, 1, 2, 3] list(0, 3, 'y') // [y, y, y, y] list(0, 3, () => 'y') // [y, y, y, y] list(0, 3, i => i) // [0, 1, 2, 3] list(0, 3, i => `y${i}`) // [y0, y1, y2, y3] list(0, 3, obj) // [obj, obj, obj, obj] list(0, 6, i => i, 2) // [0, 2, 4, 6]
max:獲取對象數組中指定標識符最大的項
- 使用說明
- 參數:目標對象數組、指定標識符的回調函數。
- 返回值:符合條件的對象。
- 使用代碼示例
import { max } from 'radash' const fish = [ { name: 'Marlin', weight: 105, source: 'ocean' }, { name: 'Bass', weight: 8, source: 'lake' }, { name: 'Trout', weight: 13, source: 'lake' } ] max(fish, f => f.weight) // => {name: "Marlin", weight: 105, source: "ocean"}
merge:合併數組中符合條件的項,並且會覆蓋第一個數組
- 使用說明
- 參數:數組1、數組2、條件函數。
- 返回值:合併覆蓋後的數組。
- 使用代碼示例
import { merge } from 'radash' const gods = [ { name: 'Zeus', power: 92 }, { name: 'Ra', power: 97 } ] const newGods = [ { name: 'Zeus', power: 100 } ] merge(gods, newGods, f => f.name) // => [{name: "Zeus" power: 100}, {name: "Ra", power: 97}]
min:獲取對象數組中指定標識符最小的項
- 使用說明
- 參數:目標對象數組、指定標識符的條件函數。
- 返回值:符合條件的的項
- 使用代碼示例
import { min } from 'radash' const fish = [ { name: 'Marlin', weight: 105, source: 'ocean' }, { name: 'Bass', weight: 8, source: 'lake' }, { name: 'Trout', weight: 13, source: 'lake' } ] min(fish, f => f.weight) // => {name: "Bass", weight: 8, source: "lake"}
objectify:根據函數映射的鍵與值把數組轉換為字典對象
- 使用說明
- 參數:目標對象數組、條件函數1用於提取鍵、[條件函數2用於提取值]
- 返回值:字典對象
- 使用代碼示例
import { objectify } from 'radash' const fish = [ { name: 'Marlin', weight: 105 }, { name: 'Bass', weight: 8 }, { name: 'Trout', weight: 13 } ] objectify(fish, f => f.name) // => { Marlin: [marlin object], Bass: [bass object], ... } objectify( fish, f => f.name, f => f.weight ) // => { Marlin: 105, Bass: 8, Trout: 13 }
range:根據步長生成一個數值範圍內的迭代值
- 使用說明
- 參數:起始值、[結束值]、[迭代函數]、步長。
- 返回值:用在for迴圈中迴圈輸出處理的結果值。
- 使用代碼示例
import { range } from 'radash' range(3) // yields 0, 1, 2, 3 range(0, 3) // yields 0, 1, 2, 3 range(0, 3, 'y') // yields y, y, y, y range(0, 3, () => 'y') // yields y, y, y, y range(0, 3, i => i) // yields 0, 1, 2, 3 range(0, 3, i => `y${i}`) // yields y0, y1, y2, y3 range(0, 3, obj) // yields obj, obj, obj, obj range(0, 6, i => i, 2) // yields 0, 2, 4, 6 for (const i of range(0, 200, 10)) { console.log(i) // => 0, 10, 20, 30 ... 190, 200 } for (const i of range(0, 5)) { console.log(i) // => 0, 1, 2, 3, 4, 5 }
replaceOrAppend:替換對象數組中的項或是追加項(條件函數不滿足時追加);
- 使用說明
- 參數:被替換數組、用來的替換的數組、條件函數。
- 返回值:替換或者追加後的數組。
- 使用代碼示例
import { replaceOrAppend } from 'radash' const fish = [ { name: 'Marlin', weight: 105 }, { name: 'Salmon', weight: 19 }, { name: 'Trout', weight: 13 } ] const salmon = { name: 'Salmon', weight: 22 } const sockeye = { name: 'Sockeye', weight: 8 } replaceOrAppend(fish, salmon, f => f.name === 'Salmon') // => [marlin, salmon (weight:22), trout] replaceOrAppend(fish, sockeye, f => f.name === 'Sockeye') // => [marlin, salmon, trout, sockeye]
replace :查找指定項,並用傳入的去替換
- 使用說明
- 參數:對象數組1、用於替換的對象、條件函數。
- 返回值:替換目標項後的原始數組副本(新數組)。
- 使用代碼示例
import { replace } from 'radash' const fish = [ { name: 'Marlin', weight: 105 }, { name: 'Bass', weight: 8 }, { name: 'Trout', weight: 13 } ] const salmon = { name: 'Salmon', weight: 22 } // read: replace fish with salmon where the name is Bass replace(fish, salmon, f => f.name === 'Bass') // => [marlin, salmon, trout]
select :對數組同時進行過濾和映射,篩選和轉換數組中的元素;
- 使用說明
- 參數:對象數組、映射條件函數、過濾條件函數。
- 返回值:過濾完後的映射值組成的數組。
- 使用代碼示例
import { select } from 'radash' const fish = [ { name: 'Marlin', weight: 105, source: 'ocean' }, { name: 'Bass', weight: 8, source: 'lake' }, { name: 'Trout', weight: 13, source: 'lake' } ] select( fish, f => f.weight, f => f.source === 'lake' ) // => [8, 13]
shift :把目標數組向右迴圈移動 n 個位置返回為一個新數組;
- 使用說明
- 參數:目標數組、移動步數。
- 返回值:移動後的新數組。
- 使用代碼示例
import { shift } from 'radash' const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] shift(arr, 3) // => [7, 8, 9, 1, 2, 3, 4, 5, 6]
sift:過濾調列表中值為false的項,返回剩餘為true的項組成的數組;
- 使用說明
- 參數:數組。
- 返回值:過濾後的新數組。
- 使用代碼示例
import { sift } from 'radash' const fish = ['salmon', null, false, NaN, 'sockeye', 'bass'] sift(fish) // => ['salmon', 'sockeye', 'bass']
sort :把數組按照條件函數指定的項的數值大小排序,支持升序和降序
- 使用說明
- 參數:對象數組、指定標識符的條件函數、是否降序(Boolean值,不傳為false)。
- 返回值:排序後的數組。
- 使用代碼示例
import { sort } from 'radash' const fish = [ { name: 'Marlin', weight: 105 }, { name: 'Bass', weight: 8 }, { name: 'Trout', weight: 13 } ] sort(fish, f => f.weight) // => [bass, trout, marlin] sort(fish, f => f.weight, true) // => [marlin, trout, bass]
sum:數組對象根據條件函數指定想的數組求和
- 使用說明
- 參數:對象數組、指定標識符的條件函數。
- 返回值:符合條件的項的和。
- 使用代碼示例
import { sum } from 'radash' const fish = [ { name: 'Marlin', weight: 100 }, { name: 'Bass', weight: 10 }, { name: 'Trout', weight: 15 } ] sum(fish, f => f.weight) // => 125
toggle:查找數組中是否有我們給定的項,有則刪除,沒有則添加;
- 使用說明
- 參數:目標數組、條件值(可以是條件函數)、在前面添加還是在後邊添加。
- 返回值:處理後的新數組。
- 使用代碼示例
import { toggle } from 'radash' // 基本用法 const gods = ['ra', 'zeus', 'loki'] toggle(gods, 'ra') // => [zeus, loki] toggle(gods, 'vishnu') // => [ra, zeus, loki, vishnu] // 切換(數組、條件項、指定標識符的條件函數) import { toggle } from 'radash' const ra = { name: 'Ra' } const zeus = { name: 'Zeus' } const loki = { name: 'Loki' } const vishnu = { name: 'Vishnu' } const gods = [ra, zeus, loki] toggle(gods, ra, g => g.name) // => [zeus, loki] toggle(gods, vishnu, g => g.name) // => [ra, zeus, loki, vishnu] // 切換(數組、條件項、條件函數,覆蓋項) import { toggle } from 'radash' const gods = ['ra', 'zeus', 'loki'] toggle(gods, 'vishnu', g => g, { strategy: 'prepend' }) // => [vishnu, ra, zeus, loki]
unique:數組去重,去除數組中重覆的項;
- 使用說明
- 參數:目標數組、指定唯一標識符的條件函數。
- 返回值:去重後的數組。
- 使用代碼示例
import { unique } from 'radash' const fish = [ { name: 'Marlin', weight: 105, source: 'ocean' }, { name: 'Salmon', weight: 22, source: 'river' }, { name: 'Salmon', weight: 22, source: 'river' } ] unique( fish, f => f.name ) // [ // { name: 'Marlin', weight: 105, source: 'ocean' }, // { name: 'Salmon', weight: 22, source: 'river' } // ]
zipToObject:將第一個數組中的鍵映射到第二個數組中對應的值
- 使用說明
- 參數:數組1、數組2(或者是傳入一個函數)。
- 返回值:映射後的對象。
- 使用代碼示例
import { zipToObject } from 'radash' const names = ['ra', 'zeus', 'loki'] const cultures = ['egypt', 'greek', 'norse'] zipToObject(names, cultures) // => { ra: egypt, zeus: greek, loki: norse } zipToObject(names, (k, i) => k + i) // => { ra: ra0, zeus: zeus1, loki: loki2 } zipToObject(names, null) // => { ra: null, zeus: null, loki: null }
zip:把兩個數組變為二維數組,二維數組中的每個數組包含兩個項分別為兩個傳入數組的相同位置的項
-
使用說明
- 參數:數組1、數組2。
- 返回值:映射後的二維數組。
-
使用代碼示例
import { zip } from 'radash' const names = ['ra', 'zeus', 'loki'] const cultures = ['egypt', 'greek', 'norse'] zip(names, cultures) // => [ // [ra, egypt] // [zeus, greek] // [loki, norse] // ]
寫在後面
- 等
Radash
庫所有方法更新完畢,作者會整理一份完整方法目錄上傳,包括思維導圖和使用目錄。 - 大家有任何問題或見解,歡迎評論區留言交流和批評指正!!!
- 你的每一個收藏都是作者寫作的動力!!!
- 點擊訪問:radash官網