Mastering JavaScript Basics-Filtering duplicate values of an array

Source: Internet
Author: User
Tags new set

Mode 1:
const unique = (value, index, arr) => {    return arr.indexOf(value) === index;}const sampleValues = [1, 4, 5, 2, ‘a‘, ‘e‘, ‘b‘, ‘e‘, 2, 2, 4];const uniqueValues = sampleValues.filter(unique);
<textarea spellcheck="false" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none" tabindex="0" readonly=""></textarea>5 1
Const Unique = (valueindexarr+= {
2
return arr. indexOf (value= =index;
3
}
4
Const samplevalues = [1452' A 'e 'b'e '2  24];
5
Const uniqueValues = samplevalues. Filter (unique);
Mode 2:
const sampleValues = [1, 4, 5, 2, ‘a‘, ‘e‘, ‘b‘, ‘e‘, 2, 2, 4];const uniqueValues = [...new Set(sampleValues)];
<textarea spellcheck="false" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none" tabindex="0" readonly=""></textarea>1
Const samplevalues = [1452' A 'e 'b'e '2 2 4];
2
Const uniqueValues = [... New Set (samplevalues)];
Author/Public number: front-end Crazy (a group of front-line programmers who love to maintain, want to change the world with the front end.) )



From for notes (Wiz)

Mastering JavaScript Basics-Filtering duplicate values of an array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.