Home / KHÁI NIỆM / tổng hợp các lodash functions mà chúng ta nên biết tới Tổng hợp các lodash functions mà chúng ta nên biết tới 14/01/2022 Lodash là gìA modern JavaScript utility library delivering modularity, performance & extras.Bạn đang xem: Tổng hợp các lodash functions mà chúng ta nên biết tớiNếu các bạn đang hoặc đã làm việc với javascript thì chắc vẫn nghe qua lodash. Một tlỗi việc rất mạnh bạo hỗ trợ tương đối nhiều hàm để xử lý data, object, strings, number tuyệt những array…. Lodash hỗ trợ performance không nhỏ với bảo vệ bình an trong số trường hòa hợp underfine, null,…. Bên cạnh đó, Khi sử dụng hàm lodash họ thấy code đẹp mắt cùng ngắn gọn rộng.Nói túm dòng váy lại là xung quanh bí quyết sử dụng những function thường thì khác ví như xử trí mảng, danh sách, string các dạng hình thì những chúng ta cũng có thể học tập thêm 1 test viện xịn xò cùng này nọ là lodash. Tớ chỉ viết ra trên đây mấy cái tuyệt cần sử dụng thôi chứ đọng nhiều trang bị các chúng ta cũng có thể đọc thêm tư liệu của lodash nghỉ ngơi đâyĐể install và áp 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");1234567// 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ịnh hành mà tớ tốt dùngXử lý danh sách.forEach(collection, .identity>)Giống với hàm foreach(), dùng làm lặp qua từng bộ phận của list và giải pháp xử lý với hàm._.forEach(<1, 2>, 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(<1, 2>, 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 những phần tử của tủ đựng đồ, trả về một mảng tất cả tất cả những vị trường đoản cú thành phần trả về quý giá true mang lại. Vị trường đoản cú được gọi cùng với ba đối số: (value, index | key, collection).var users = < "user": "barney", "age": 36, "active": true , "user": "fred", "age": 40, "active": false >; _.filter(users, function(o) return !o.active; );// => objects for <"fred"> // The `_.matches` iteratee shorth&._.filter(users, "age": 36, "active": true );// => objects for <"barney"> // The `_.matchesProperty` iteratee shorthvà._.filter(users, <"active", false>);// => objects for <"fred"> // The `_.property` iteratee shorthvà._.filter(users, "active");// => objects for <"barney">1234567891011121314151617181920var users = < "user": "barney", "age": 36, "active": true , "user": "fred", "age": 40, "active": false >;_.filter(users, function(o) return !o.active; );// => objects for <"fred">// The `_.matches` iteratee shorth&.Xem thêm: Die For Là Gì - Không Phải Đâu Em Ơi!_.filter(users, "age": 36, "active": true );// => objects for <"barney">// The `_.matchesProperty` iteratee shorth&._.filter(users, <"active", false>);// => objects for <"fred">// The `_.property` iteratee shorthvà._.filter(users, "active");// => objects for <"barney">.find(collection, .identity>, )Lặp lại các bộ phận của tủ chứa đồ, trả về vị trường đoản cú thành phần trước tiên trả về quý giá true đến. Vị trường đoản cú được Gọi với ba đối số: (value, index | key, collection).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 shorthvà._.find(users, "age": 1, "active": true );// => object for "pebbles" // The `_.matchesProperty` iteratee shorthvà._.find(users, <"active", false>);// => 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 < 40; );// => object for "barney"// The `_.matches` iteratee shorth&._.find(users, "age": 1, "active": true );// => object for "pebbles"// The `_.matchesProperty` iteratee shorth&._.find(users, <"active", false>);// => object for "fred"// The `_.property` iteratee shorth&._.find(users, "active");// => object for "barney".findLast(collection, .identity>, )Hàm này giống như _.find ngoại trừ câu hỏi nó tái diễn các thành phần của bộ sưu tầm từ bỏ phải sang trái._.findLast(<1, 2, 3, 4>, function(n) return n % 2 == 1;);// => 312345_.findLast(<1, 2, 3, 4>, function(n) return n % 2 == 1;);// => 3_.includes(collection, value, )Kiểm tra coi cực hiếm gồm nằm trong danh sách hay là không. Nếu tập thích hợp là một trong những chuỗi, nó sẽ được bình chọn để kiếm tìm một chuỗi bé có mức giá trị, nếu như không thì SameValueZero được thực hiện để đối chiếu đồng đẳng. Nếu fromIndex là số âm, nó được áp dụng làm phần bù bỏ phần cuối của tủ đồ._.includes(<1, 2, 3>, 1);// => true _.includes(<1, 2, 3>, 1, 2);// => false _.includes( "a": 1, "b": 2 , 1);// => true _.includes("abcd", "bc");// => true123456789101112_.includes(<1, 2, 3>, 1);// => true_.includes(<1, 2, 3>, 1, 2);// => false_.includes( "a": 1, "b": 2 , 1);// => true_.includes("abcd", "bc");// => true.map(collection, .identity>)Cũng tương đương với foreach lặp qua những phần tử vào danh sách cơ mà bao gồm trả về một list new.function square(n) return n * n; _.map(<4, 8>, square);// => <16, 64> _.map( "a": 4, "b": 8 , square);// => <16, 64> (iteration order is not guaranteed) var users = < "user": "barney" , "user": "fred" >; // The `_.property` iteratee shorthand._.map(users, "user");// => <"barney", "fred">12345678910111213141516171819function square(n) return n * n;_.map(<4, 8>, square);// => <16, 64>_.map( "a": 4, "b": 8 , square);// => <16, 64> (iteration order is not guaranteed)var users = < "user": "barney" , "user": "fred" >;// The `_.property` iteratee shorthvà._.map(users, "user");// => <"barney", "fred">Thôi liệt kê mệt nhọc quá cơ mà lodash support không ít function cho tất cả các thể một số loại array, collection, function, date, lang, math, number, object, seq, string, util, properties, methods.Nguồn tmê say khảo: https://lodash.com/docs/4.17.15Chia sẻ bài viết ngay Nguồn bài viết : Viblo Quý khách hàng gồm hứng thụ thay đổi việc không? trăng tròn Front-End Developers - Titan Technology Corporation JavaScript AngularJS ReactJS Thỏa thuận Prúc Nhuận, TPhường Hồ Chí Minh Back-over Developer - ASIM Telecom JavaScript NodeJS Adobe InDesign 800 USD - 1,600 USD Tân Bình, TPhường Hồ Chí Minh đôi mươi Frontkết thúc Developers - Smartphone World JavaScript VueJS ReactJS 600 USD - 2,000 USD Tân Bình, TP Hồ Chí Minh Java Engineer - 7-Eleven Vietnam Java Spring JavaScript Thỏa thuận Quận 1, TPhường Hồ Chí Minh JavaScript/TypeScript Developer - TIBCO JavaScript TypeScript UI Lên đến1,500 USD Tkhô hanh Xuân, Hà Nội Cập nhật thông tin mới nhất và phổ biến Tin new nhất iPhone 14 Pro và 14 Pro Max lộ giá thành cao hơn nữa trước Tháng Một 13, 2022 FPT Shop bớt mang đến 3 triệu cho máy vi tính gaming, tặng thêm loài chuột gaming 270 nghìn đồng Tháng Một 13, 2022 Tin tức phổ biến Hướng dẫn sử dụng ngữ điệu R cho tất cả những người new bước đầu Tháng Sáu đôi mươi, 2014 10 plugin quan trọng của SublimeText dành cho những thiết kế viên JavaScript Tháng Chín 04, 2015