Lodash là gì

A modern JavaScript utility library delivering modularity, performance & extras.

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

Nếu các bạn đã hoặc đang làm việc với javascript thì chắc đã nghe qua lodash. Một thư việc rất mạnh mẽ cung cấp rất nhiều hàm để xử lý data, object, strings, number hay các array…. Lodash cung cấp performance rất cao và đảm bảo an toàn trong các trường hợp underfine, null,…. Ngoài ra, khi sử dụng hàm lodash chúng ta thấy code đẹp và ngắn gọn hơn.

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

Để install và sử dụng lodash tại npm hoặc yarn

// Load the full build.var _ = require(“lodash”);// Load the core build.var _ = require(“lodash/core”);// Load the FP build for immutable auto-curried iteratee-first data-last methods.var fp = require(“lodash/fp”);
1
2
3
4
5
6
7
// Load the full build.
var _ = require(“lodash”);
// Load the 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à tớ hay dùng

Xử lý danh sách

.forEach(collection, .identity>)

Giống với hàm foreach(), dùng để lặp qua mỗi phần tử của danh sách và 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).
1
2
3
4
5
6
7
8
9
10
_.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 phần tử của bộ sưu tập, trả về một mảng gồm tất cả các vị từ phần tử trả về giá trị true cho. Vị từ được gọi 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var 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: Từ Vựng Cho Bộ Phận Tuyển Dụng Tiếng Anh Là Gì

_.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 phần tử của bộ sưu tập, trả về vị từ phần tử đầu tiên trả về giá trị true cho. Vị từ được gọi với ba đố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”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var 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 giống như _.find ngoại trừ việc nó lặp lại các phần tử của bộ sưu tập từ phải sang trái.

_.findLast(, function(n) { return n % 2 == 1;});// => 3
1
2
3
4
5
_.findLast(, function(n) {
return n % 2 == 1;
});
// => 3

_.includes(collection, value, )

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

_.includes(, 1);// => true _.includes(, 1, 2);// => false _.includes({ “a”: 1, “b”: 2 }, 1);// => true _.includes(“abcd”, “bc”);// => true
1
2
3
4
5
6
7
8
9
10
11
12
_.includes(, 1);
// => true
_.includes(, 1, 2);
// => false
_.includes({ “a”: 1, “b”: 2 }, 1);
// => true
_.includes(“abcd”, “bc”);
// => true

.map(collection, .identity>)

Cũng giống với foreach lặp qua các phần tử trong danh sách nhưng có trả về một danh sách 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”);// =>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function 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: Ethos Là Gì – Nghĩa Của Từ Ethos Trong Tiếng Việt

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

Thôi liệt kê mệt quá cơ mà lodash support rất 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.

Nguồn tham khảo: https://lodash.com/docs/4.17.15

Chia sẻ bài viết ngay

Nguồn bài viết : Viblo

Bạn có hứng thú đổi việc không?

Senior Frontend Developer – AG Corp

JavaScript AngularJS UI/UX
Thỏa thuận
Cầu Giấy, Hà Nội

Tech Manager (.NET) – Citigo Software – KiotvietHN

.NET Golang JavaScript
Lên đến2,300 USD
Hoàn Kiếm, Hà Nội

3 Backend Developers – Shopstack Vietnam

PHP Magento JavaScript
Thỏa thuận
Thanh Xuân, Hà Nội

Magento Frontend Developer – Moni Media

Magento HTML5 JavaScript
Thỏa thuận
Tan Binh, TP Hồ Chí Minh

Magento Web Designer – Moni Media

Magento JavaScript ReactJS
Thỏa thuận
Tan Binh, TP Hồ Chí Minh

*

Cập nhật tin tức mới nhất và phổ biến

Tin mới nhất

Vì sao nhiều dự án sử dụng Tailwind CSS

Tháng Hai 09, 2021

Bàn phím trong ứng dụng tùy chỉnh trong Flutter

Tháng Hai 09, 2021

Tin tức phổ biến

10 plugin cần thiết của SublimeText dành cho các lập trình viên JavaScript

Tháng Chín 04, 2015

Chuyên mục: Hỏi Đáp