JavaScript筆記--Lodash

        Lodash是一個著名的javascript原生庫,不需要引入其他第三方依賴。是一個意在提高開發者效率,提高JS原生方法性能的JS庫。簡單的說就是,很多方法lodash已經幫你寫好了,直接調用就行, Lodash使用了一個簡單的 _ 符號,就像Jquery的 $ 一樣,十分簡潔。爲什麼使用lodash,通過使用數組,數字,對象,字符串等方法,Lodash使JavaScript變得更簡單。

官網:https://www.lodashjs.com/

  npm i --save lodash
 或 yarn  add  lodash

ReactNative中使用 如下:

import _ from 'lodash';
 
 formatImgs() {
        const ImgItems: any = [];
        console.log(' formatImgs ', this.state.badgeData.length);
        _.forEach(this.state.badgeData, (item: any) => {
            ImgItems.push(
                <View style={styles.itemContentContainer}>
                    <Image style={styles.itemImagDetail} source={showIntegralActions(item.badge_image_url)} />
                    <Text>{item.badgeTitle}</Text>
                </View>
            );
        });
        return ImgItems;
    }

JavaScript中使用:

遍歷循環執行某個方法
_.map()

<script type="text/javascript">
    function square(n) {
        return n * n;
    }

    console.log(_.map([4, 8], square));
    // => [16, 64]

    console.log(_.map({ 'a': 4, 'b': 8 }, square));
    // => [16, 64] (iteration order is not guaranteed)

    var users = [
        { 'user': 'baidu' },
        { 'user': 'google' }
    ];

    console.log(_.map(users, 'user'));
    // => ['baidu', 'google']
</script>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章