數(shù)組函數(shù)
數(shù)組是集合中的特例,有著優(yōu)秀的品質(zhì)。從抽象數(shù)據(jù)模型角度,可以模擬棧,隊(duì)列,hash表,樹的操作。實(shí)際在樹的方法實(shí)現(xiàn)中也遵循了這種分類方式。
Underscore 不僅處理了規(guī)范的數(shù)組,同時(shí)也將這種能力拓展給了 arguments 類數(shù)組。
當(dāng)然,前文針對(duì)集合的方法對(duì)于數(shù)組也同樣適用。
構(gòu)造數(shù)組 _.range([start], stop, [step])
一個(gè)用來創(chuàng)建整數(shù)靈活編號(hào)的列表的函數(shù)
_.range(10); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
_.range(1, 11); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
_.range(0, 30, 5); // [0, 5, 10, 15, 20, 25]
_.range(0, -10, -1); // [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
_.range(0); // []
數(shù)組索引
除了數(shù)組內(nèi)置的方法,Underscore 提供了三個(gè)索引的封裝
_.indexOf(array, value, [isSorted])
返回value在該 array 中的索引值,如果value不存在 array中就返回-
_.indexOf([1, 2, 3], 2); //1
_.lastIndexOf(array, value, [fromIndex])
返回value在該 array 中的從最后開始的索引值,如果value不存在 array中就返回-1
_.sortedIndex(list, value, [iteratee], [context])
使用二分查找確定value在list中的位置序號(hào),value按此序號(hào)插入能保持list原有的排序。 如果提供iterator函數(shù),iterator將作為list排序的依據(jù),包括你傳遞的value 。 iterator也可以是字符串的屬性名用來排序(比如length)。
var stooges = [{name: 'moe', age: 40}, {name: 'curly', age: 60}];
_.sortedIndex([10, 20, 30, 40, 50], 35); // 3
_.sortedIndex(stooges, {name: 'larry', age: 50}, 'age'); // 1
數(shù)組轉(zhuǎn)化為對(duì)象 _.object(list, [values])
list 作為對(duì)象的 key,values 作為對(duì)象屬性的 value, 如果 key 值重復(fù),后邊的 key值會(huì)覆蓋前邊的 key值。
_.object(['moe', 'larry', 'curly'], [30, 40, 50]);
=> {moe: 30, larry: 40, curly: 50}
_.object([['moe', 30], ['larry', 40], ['curly', 50]]);
=> {moe: 30, larry: 40, curly: 50}
數(shù)組截取
_.first(array, [n])
別名 _.head() _.take()
返回?cái)?shù)組中前 n 個(gè)元素所組成的數(shù)組,n 不傳則為 1,返回第一個(gè)元素(非數(shù)組)。
_.first([1,2,3,4,5], 2) // [1,2]
反方向就是 _.last(array, [n])
返回?cái)?shù)組中從后向前的 n 個(gè)元素,規(guī)則與 first 相同
_.initial(array, [n])
排除數(shù)組后邊的 n 個(gè)元素,n 不傳則為1,返回除了尾部元素以外的剩余元素?cái)?shù)組
反方向就是 _.rest(array, [index])
返回 index 以后的所有元素,index 不傳則為 1
數(shù)組清洗
去偽存真 _.compact(array)
返回一個(gè)除去所有false值的 array副本,副本中無假值。可用于提取稀疏數(shù)組中的內(nèi)容。
_.compact([0, 1, false, 2, '', 3]); // [1, 2, 3]
數(shù)組拓?fù)?_.flatten(array, [shallow])
將一個(gè)嵌套多層的數(shù)組 array(數(shù)組) (嵌套可以是任何層數(shù))轉(zhuǎn)換為只有一層的數(shù)組。 如果你傳遞 shallow參數(shù),數(shù)組將只減少一維的嵌套。
_.flatten([1, [2], [3, [[4]]]]); //[1, 2, 3, 4];
_.flatten([1, [2], [3, [[4]]]], true); //[1, 2, 3, [[4]]];
矩陣置換 _.zip(...array)
將 每個(gè)arrays中相應(yīng)位置的值合并在一起
_.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
=> [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]
刪除指定值 _.without(array, ...value)
返回一個(gè)刪除所有values值后的 array副本。(注:使用===表達(dá)式做相等測(cè)試。)
_.without([1, 2, 1, 0, 3, 1, 4], 0, 1); //[2, 3, 4]
除重 _.uniq(array, [isSorted], [iteratee])
別名: unique
返回 array去重后的副本, 使用 === 做相等測(cè)試. 如果您確定 array 已經(jīng)排序, 那么給 isSorted 參數(shù)傳遞 true值, 此函數(shù)將運(yùn)行的更快的算法. 如果要處理對(duì)象元素, 傳參 iterator 來獲取要對(duì)比的屬性.
_.uniq([1, 2, 1, 3, 1, 4]); //[1, 2, 3, 4]
數(shù)組運(yùn)算
并集 _.union(*arrays)
返回傳入的 arrays(數(shù)組)并集:按順序返回,返回?cái)?shù)組的元素是唯一的,可以傳入一個(gè)或多個(gè) arrays(數(shù)組)。
_.union([1, 2, 3], [101, 2, 1, 10], [2, 1]); //[1, 2, 3, 101, 10]
交集 _.intersection(*arrays)
返回傳入 arrays(數(shù)組)交集。結(jié)果中的每個(gè)值是存在于傳入的每個(gè)arrays(數(shù)組)里。
_.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]); // [1, 2]
差集 _.difference(array, *others)
類似于without,但返回的值來自array參數(shù)數(shù)組,并且不存在于other 數(shù)組.
_.difference([1, 2, 3, 4, 5], [5, 2, 10]); //[1, 3, 4]