Lodash Là Gì

     

Lodash là gì

A modern JavaScript utility library delivering modularity, performance và extras.

Bạn đang xem: Lodash là gì

Bạn vẫn xem: Lodash là gì

Nếu các bạn đã hoặc đang thao tác với javascript thì vững chắc đã nghe qua lodash. Một thư việc rất mạnh mẽ cung ứng rất những hàm để cách xử trí data, object, strings, number hay những array…. Lodash hỗ trợ performance không hề nhỏ và đảm bảo an toàn trong những trường thích hợp underfine, null,…. Ngoài ra, khi thực hiện hàm lodash họ thấy code đẹp với ngắn gọn gàng hơn.

Nói túm loại váy lại là kế bên cách dùng các function thường thì khác như giải pháp xử lý mảng, danh sách, string những kiểu thì các bạn có thể học thêm một thử viện xịn xò với này nọ là lodash. Tớ chỉ viết ra trên đây mấy cái hay sử dụng thôi chứ những thứ chúng ta có thể tìm hiểu thêm tài liệu của lodash ở đây

Để install và thực hiện lodash tại npm hoặc yarn

// Load the full build.var _ = require(“lodash”);// Load the bộ vi xử lý core build.var _ = require(“lodash/core”);// Load the FP build for immutable auto-curried iteratee-first data-last methods.var fp = require(“lodash/fp”);1234567// Load the full build.var _ = require(“lodash”);// Load the vi xử lý core build.var _ = require(“lodash/core”);// Load the FP build for immutable auto-curried iteratee-first data-last methods.var fp = require(“lodash/fp”);

Một số hàm thông dụng mà lại tớ hay dùng

Xử lý danh sách

.forEach(collection, .identity>)

Giống cùng với hàm foreach(), dùng làm lặp qua mỗi phần tử của list và giải pháp xử lý với hàm.

_.forEach(, function(value) console.log(value););// => Logs `1` then `2`. _.forEach( “a”: 1, “b”: 2 , function(value, key) console.log(key););// => Logs “a” then “b” (iteration order is not guaranteed).12345678910_.forEach(, function(value) console.log(value););// => Logs `1` then `2`._.forEach( “a”: 1, “b”: 2 , function(value, key) console.log(key););// => Logs “a” then “b” (iteration order is not guaranteed).

.filter(collection, .identity>)

Lặp lại các thành phần của cỗ sưu tập, trả về một mảng gồm tất cả các vị từ bộ phận trả về quý hiếm true cho. Vị từ bỏ được hotline với ba đối số: (value, index | key, collection).

var users = ; _.filter(users, function(o) return !o.active; );// => objects for // The `_.matches` iteratee shorthand._.filter(users, “age”: 36, “active”: true );// => objects for // The `_.matchesProperty` iteratee shorthand._.filter(users, );// => objects for // The `_.property` iteratee shorthand._.filter(users, “active”);// => objects for 1234567891011121314151617181920var users = “user”: “barney”, “age”: 36, “active”: true , “user”: “fred”, “age”: 40, “active”: false >;_.filter(users, function(o) return !o.active; );// => objects for // The `_.matches` iteratee shorthand.

Xem thêm: Plasma Là Gì - Công Nghệ Diệt Khuẩn Khử Mùi Plasma

_.filter(users, “age”: 36, “active”: true );// => objects for // The `_.matchesProperty` iteratee shorthand._.filter(users, );// => objects for // The `_.property` iteratee shorthand._.filter(users, “active”);// => objects for

.find(collection, .identity>, )

Lặp lại các bộ phận của cỗ sưu tập, trả về vị từ bộ phận đầu tiên trả về quý giá true cho. Vị trường đoản cú được call với cha đối số: (value, index | key, collection).

var users = ; _.find(users, function(o) { return o.age object for “barney” // The `_.matches` iteratee shorthand._.find(users, “age”: 1, “active”: true );// => object for “pebbles” // The `_.matchesProperty` iteratee shorthand._.find(users, );// => object for “fred” // The `_.property` iteratee shorthand._.find(users, “active”);// => object for “barney”123456789101112131415161718192021var users = “user”: “barney”,”age”: 36, “active”: true , “user”: “fred”,”age”: 40, “active”: false , “user”: “pebbles”, “age”: 1,”active”: true >;_.find(users, function(o) { return o.age // => object for “barney”// The `_.matches` iteratee shorthand._.find(users, “age”: 1, “active”: true );// => object for “pebbles”// The `_.matchesProperty` iteratee shorthand._.find(users, );// => object for “fred”// The `_.property` iteratee shorthand._.find(users, “active”);// => object for “barney”

.findLast(collection, .identity>, )

Hàm này y như _.find ngoại trừ vấn đề nó lặp lại các thành phần của tủ chứa đồ từ đề xuất sang trái.

_.findLast(, function(n) return n % 2 == 1;);// => 312345_.findLast(, function(n) return n % 2 == 1;);// => 3

_.includes(collection, value, )

Kiểm tra xem giá bán trị có thuộc danh sách hay không. Giả dụ tập hợp là 1 chuỗi, nó sẽ tiến hành kiểm tra nhằm tìm một chuỗi con có mức giá trị, nếu như không thì SameValueZero được thực hiện để so sánh bình đẳng. Giả dụ fromIndex là số âm, nó được sử dụng làm phần bù dồn phần cuối của cục sưu tập.

_.includes(, 1);// => true _.includes(, 1, 2);// => false _.includes( “a”: 1, “b”: 2 , 1);// => true _.includes(“abcd”, “bc”);// => true123456789101112_.includes(, 1);// => true_.includes(, 1, 2);// => false_.includes( “a”: 1, “b”: 2 , 1);// => true_.includes(“abcd”, “bc”);// => true

.map(collection, .identity>)

Cũng tương tự với foreach lặp qua các phần tử trong list nhưng gồm trả về một list mới.

function square(n) return n * n; _.map(, square);// => _.map( “a”: 4, “b”: 8 , square);// => (iteration order is not guaranteed) var users = ; // The `_.property` iteratee shorthand._.map(users, “user”);// => 12345678910111213141516171819function square(n) return n * n;_.map(, square);// => _.map( “a”: 4, “b”: 8 , square);// => (iteration order is not guaranteed)var users = “user”: “barney” , “user”: “fred” >;// The `_.property` iteratee shorthand.

Xem thêm: Từ Điển Tiếng Việt " Giản Dị Là Gì ? Biểu Hiện Cùng Ý Nghĩa Của Lối Sống Giản Dị

_.map(users, “user”);// =>

Thôi liệt kê mệt quá nhưng lodash support tương đối nhiều function cho tất cả các thể loại array, collection, function, date, lang, math, number, object, seq, string, util, properties, methods.