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