Skip to main content

Xóa phần tử Duplicates trong mảng sử dụng ES6


Dưới đây là 3 cách để lọc các phần tử trùng nhau (duplicates) trong mảng và trả về những giá trị duy nhất. Tôi thích sử dụng Set bởi vì nó ngắn và đơn giản.

1. Sử dụng Set:

Đầu tiên sẽ tìm hiểu Set là gì:

Set là một đối tượng mới được giới thiệu trong ES6. Bởi vì Set chỉ cho lưu trữ các giá trị duy nhất. Khi bạn truyền vào nó 1 mảng thì nó sẽ xóa những giá trị trùng lặp.

Okay, chúng ta sẽ nhìn những đoạn code sau để hiểu hơn về Set:

1. Đầu tiên, chúng ta sẽ tạo 1 Set mới bằng cách truyền vào cho nó 1 mảng. Bởi vì Set chỉ nhận các giá trị duy nhất lên sẽ xóa các phần tử trùng lặp.

2. Bây giờ các phần tử trùng nhau biến mất, chúng ta sẽ convert nó lại thành mảng bằng cách sử dụng toán tử ...



Convert Set sang mảng sử dụng Array.from

Ngoài ra, bạn có thể sử dụng Array.from để convert Set sang mảng:



2. Sử dụng filter:

Để hiểu hơn về cách này, chúng ta sẽ xem qua cách 2 phương thức hoạt động: indexOf và filter.

indexOf

Phương thức indexOf sẽ trả về vị trí index đầu tiên mà chúng tìm thấy trong mảng.



filter

Phương thức filter() sẽ tạo ra 1 mảng mới với các điều kiện cho trước. Nói cách khác, các phần tử thỏa mãn điều kiện trả về true thì sẽ nằm trong mảng đã được lọc. Các phần tử sai điều kiện sẽ bị loại bỏ trong mảng mới.



Dưới đây là bảng console.log đoạn code trên:



3. Sử dụng reduce:

Trong trường hợp này, hàm reduce sẽ kiểm tra mảng xem có phần tử đó hay không. Nếu chưa có sẽ thêm phần tử đó vào mảng, còn không sẽ bỏ qua phần tử đó.

Hàm reduce hơi khó hiểu một chút, vì vậy hãy xem thử ví dụ sau:



Và đây là kết quả console.log:



Thanks for reading ❤

Resource: Medium Dailyjs

Comments

Post a Comment

Popular posts from this blog

Hacking Facebook using Social Engineering Tools 2017

Hacking Facebook using Social Engineering Tools 2017 Hôm nay tôi xin demo cho các bạn xem làm như thế nào để Hacking được Facebook bằng Kali Linux. Việc hack này chỉ thực hiện được trong mạng LAN chứ không phải ở bất cứ đâu. Nếu bạn thấy hay thích hãy Like và Sub nhé ! Chúc bạn thành công !

Testing React Native Apps

What are we unit testing exactly ? We're using  "unit testing"  to refer to tests of functions and plain JavaScript objects, independent of the React Native framework. This means that we aren't testing any components that rely on React Native. For example, a unit could be individual methods and functions in classes or really any small pieces of functionality. We mock out dependencies in these tests so that we can test individual methods and functions in isolation. These test are written using testing frameworks and for this article i will be using  Jest , javascript testing framework together with  Enzyme  and  React Native Testing Library . Setting Install If you use  React Native CLI  installs the  Jest  testing framework by default. But if you're using Expo we need to install it manually. yarn add -D enzyme enzyme-adapter-react-16 More: yarn add react-dom react-native-testing-library Create new file  je...

Content Injection Vulnerability in WordPress

Content Injection Vulnerability in WordPress As part of a vulnerability research project for our Sucuri Firewall (WAF) , we have been auditing multiple open source projects looking for security issues. While working on WordPress, we discovered was a severe content injection (privilege escalation) vulnerability affecting the REST API. This vulnerability allows an unauthenticated user to modify the content of any post or page within a WordPress site. We disclosed the vulnerability to the WordPress Security Team who handled it extremely well. They worked closely with us to coordinate the disclosure timeline and get as many hosts and security providers aware and patched before this became public. A fix for this was silently included on version 4.7.2 along with other less severe issues. This was done intentionally to give everyone time to patch. We are now disclosing the details because we feel there has been enough time for most WordPress users to update their sites. Ar...