學習筆記:_lodash.js常用函數


 

_lodash.js 

文檔:https://www.lodashjs.com/docs/4.17.5.html

 

_.compact(array)

 創建一個移除了所有假值的數組

什么是假值?false,null,0,"",undefined,NaN

_.compact([0, 1, false, 2, '', 3]);

// => [1, 2, 3]

 

_.concat(array, [values])

 創建一個用任何數組或值連接的新數組

var array = [1];
var other = _.concat(array, 2, [3], [[4]]);

console.log(other);
// => [1, 2, 3, [4]]

console.log(array);
// => [1]

 

_.indexOf(array, value, [fromIndex=0])

 返回數組中首次匹配的index

_.indexOf([1, 2, 1, 2], 2);
// => 1

_.indexOf([1, 2, 1, 2], 2, 2);
// => 3

 

_.join(array, [separator=','])

 將數組中的所有元素轉換為由separator分隔的字符串

_.join(['a', 'b', 'c'], '~');
// => 'a~b~c'

 

_.forEach(collection, [iteratee=_.identity])

 遍歷集合

_([1, 2]).forEach(function(value){
   console.log(value); 
});
// => 輸出 '1' 和 '2'

_.forEach({ 'a': 1, 'b': 2 }, function(value, key) {
   console.log(key); 
});
// => 輸出 'a' 和 'b' (不保證遍歷得順序)

 

 

 

(未完)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM