Js deletes duplicate elements in the array and retains one (two implementation ideas). js Array

Source: Internet
Author: User
Tags ming

Js deletes duplicate elements in the array and retains one (two implementation ideas). js Array

For example, var student = ['qiang ', 'ming', 'Tao', 'lil', 'liang', 'you', 'qiang ', 'Tao'];

The first approach is:Traverse the array arr to be deleted and put the elements into the tmp of another array. The element can be put into tmp only when it is determined that the element does not exist in arr.

Two functions are used: for... in and indexOf ()

<Script type = "text/javascript"> var student = ['qiang ', 'ming', 'Tao', 'lil', 'liang', 'you ', 'qiang ', 'Tao']; function unique (arr) {// traverses arr and places the elements in the tmp Array (if they do not exist) var tmp = new Array (); for (var I in arr) {// if (tmp. indexOf (arr [I]) =-1) {tmp. push (arr [I]) ;}} return tmp ;}</script>

The second approach is:Changing the element values and key positions of the target array arr will automatically delete the repeated elements. After the replacement, the array ('qiang '=> 1, 'ming' => 1, 'Tao' => 1)

<Script type = "text/javascript"> var student = ['qiang ', 'ming', 'Tao', 'lil', 'liang', 'you ', 'qiang ', 'Tao']; function unique (arr) {var tmp = new Array (); for (var m in arr) {tmp [arr [m] = 1;} // Replace the location of the key and value with var tmparr = new Array (); for (var n in tmp) {tmparr. push (n) ;}return tmparr ;}</script>

Javascript defines a function (removing repeated elements of an array) in the prototype of an array object, so that all array objects can apply this method.

Function box (arr) {this. arr = arr ;}
Box. prototype. aa = function (){
This. ori = []. concat (this. arr );
For (var I = 0; I <this. arr. length; I ++)
For (var j = I + 1; j <this. arr. length; j ++)
If (this. arr [I] = this. arr [j])
This. arr. splice (j, 1 );
Alert ("the original is:" + this. ori + "\ n" + "after deduplication:" + this. arr );
Var num = new box ([,]);
Num. aa ();

Remove duplicate values from the array in JS

If you simplify the test array to [1, 2, "2", 2, 3], and use console. log output in each step, you can see it clearly.
In the function, typeof is used to distinguish 2 from "2" (number and string)

Array. prototype. del = function () {var a = {}, c = [], l = this. length; for (var I = 0; I <l; I ++) {var B = this [I]; // for 1st cycles: 1 // for Loop 2nd times: 2 // for Loop 3rd Times: "2" // for Loop 4th times: 2 // for Loop 5th times: 3 console. log (B); var d = (typeof B) + B; // for Loop 1st times: number1 // for Loop 2nd times: number2 <--- duplicate // for Loop 3rd Times: string2 // for Loop 4th times: number2 <--- duplicate // for Loop 5th times: number3 console. log (d); // for Loop 1st times: number1 is undefined // for Loop 2nd times: number2 is undefined // for Loop 3rd Times: string2 is undefined // for Loop 4th times: note that number2 is already in {}, so it is not undefined // for Loop 5th times: number3 is undefined console. log (a [d]); // a [d] indicates the attribute that sets d to a, for example, a = {number2 }, but there is no value if (a [d] === undefined) {// 1, 2, "2", 3 is pushed c. push (B); // here = 1 is only a value for this attribute, for example, a = {number2: 1} a [d] = 1; // for 1st cycles: {number1 = 1} // for Loop 2nd times: {number1 = 1, number2 = 1} // for Loop 3rd Times: {number1 = 1, number2 = 1, string2 = 1} // for Loop 4th times: Because number2 exists, this time it is ...... remaining full text>

Related Article

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.