Home >Web Front-end >JS Tutorial >Five ways to remove duplicates from JavaScript arrays_javascript skills

Five ways to remove duplicates from JavaScript arrays_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:33:301202browse

Javascript array deduplication is a relatively common requirement, and there are many solutions. The answers can be found online. Below, the editor has compiled a guide for you to deduplicate arrays of the same type. Method, first introduce to you a simple implementation idea.

Things:

Traverse the array and compare one by one. If the comparison is the same, delete the following

Traverse the array, compare one by one, skip the previous duplicates, and put the different ones into the new array

Put any array element into the new array, traverse the remaining array elements, select any one, compare it one by one with the elements of the new array, if there are differences, put it into the new array.

Traverse the array, take an element as an attribute of the object, and determine whether the attribute exists

1. Delete the following duplicates:

function ov(arr){
 //var a=((new Date).getTime())
 for(var i=;i<arr.length;i++)
 for(var j=i+;j<arr.length;j++)
  if(arr[i]===arr[j]){arr.splice(j,);j--;}  
 //console.info((new Date).getTime()-a)  
 return arr.sort(function(a,b){return a-b});
}

2. This is a conventional method, which is easier to understand. If they are the same, jump out of the loop

function ov(a) {
 //var a=((new Date).getTime())
 var b = [], n = a.length, i, j;
 for (i = ; i < n; i++) {
 for (j = i + ; j < n; j++)
  if (a[i] === a[j]){j=false;break;}
 if(j)b.push(a[i]);
 }
 //console.info((new Date).getTime()-a) 
 return b.sort(function(a,b){return a-b});
}

3. It took me a long time to understand this. Although the j loop continues here, the i value has changed. It is equivalent to a new i loop:

function ov(a) {
 //var a=((new Date).getTime())
 var b = [], n = a.length, i, j;
 for (i = ; i < n; i++) {
 for (j = i + ; j < n; j++)
 if (a[i] === a[j])j=++i
 b.push(a[i]);}
 //console.info((new Date).getTime()-a) 
 return b.sort(function(a,b){return a-b});
}

4. Ensure that everything in the new array is unique

function ov(ar){
//var a=((new Date).getTime())
 var m=[],f;
 for(var i=;i<ar.length;i++){
 f=true;
 for(var j=;j<m.length;j++)
 if(ar[i]===m[j]){f=false;break;};
 if(f)m.push(ar[i])}
//console.info((new Date).getTime()-a) 
 return m.sort(function(a,b){return a-b});
}

5. Use object attributes

function ov(ar){
// var a=(new Date).getTime()
 var m,n=[],o= {};
 for (var i=;(m= ar[i])!==undefined;i++)
 if (!o[m]){n.push(m);o[m]=true;}
// console.info((new Date).getTime()-a) 
 return n.sort(function(a,b){return a-b});;
 }

3 properties of javascript array object

1. length attribute

The Length attribute represents the length of the array, that is, the number of elements in it. Because the index of an array always starts from 0, the upper and lower limits of an array are: 0 and length-1 respectively. Unlike most other languages, the length property of JavaScript arrays is variable, which requires special attention. When the length attribute is set larger, the state of the entire array does not actually change, only the length attribute becomes larger; when the length attribute is set smaller than the original, the elements in the original array with indexes greater than or equal to length will All values ​​are lost. The following is an example demonstrating changing the length attribute:

var arr=[12,23,5,3,25,98,76,54,56,76];

//Defines an array containing 10 numbers

alert(arr.length); //Display the length of the array 10

arr.length=12; //Increase the length of the array

alert(arr.length); //Shows that the length of the array has become 12

alert(arr[8]); //Display the value of the 9th element, which is 56

arr.length=5; //Reduce the length of the array to 5, and elements with indexes equal to or exceeding 5 are discarded

alert(arr[8]); //Show that the 9th element has become "undefined"

arr.length=10; //Restore the array length to 10

alert(arr[8]); //Although the length is restored to 10, the 9th element cannot be recovered and displays "undefined"

From the above code we can clearly see the nature of the length attribute. But the length object can not only be set explicitly, it may also be modified implicitly. You can use an undeclared variable in JavaScript. Similarly, you can also use an undefined array element (referring to an element with an index greater than or equal to length). In this case, the value of the length attribute will be set to the value of the element index used. Add 1. For example, the following code:

var arr=[12,23,5,3,25,98,76,54,56,76];
alert(arr.length);
arr[15]=34;
alert(arr.length);

The code also defines an array containing 10 numbers. It can be seen from the alert statement that its length is 10. Then the element with index 15 is used and assigned a value of 15, that is, arr[15]=34. At this time, the alert statement is used to output the length of the array, and the result is 16. Regardless, this is a surprising feature for developers accustomed to strongly typed programming. In fact, the initial length of an array created using new Array() is 0. It is the operation of undefined elements that causes the length of the array to change.

As you can see from the above introduction, the length attribute is so magical. You can use it to easily increase or decrease the capacity of the array. Therefore, an in-depth understanding of the length attribute will help to use it flexibly during the development process.

2. prototype attribute

Returns a reference to the prototype of the object type. The prototype property is common to object.

objectName.prototype

The objectName parameter is the name of the object object.

Description: Use the prototype attribute to provide a set of basic functions of the object's class. New instances of an object "inherit" the operations assigned to the object's prototype.

For array objects, the following example illustrates the use of the prototype attribute.

Add a method to the array object that returns the maximum element value in the array. To accomplish this, declare a function, add it to Array.prototype, and use it.

function array_max( )
{
 var i, max = this[0];
 for (i = 1; i < this.length; i++)
 {
 if (max < this[i])
 max = this[i];
 }
 return max;
}
Array.prototype.max = array_max;
var x = new Array(1, 2, 3, 4, 5, 6);
var y = x.max( );

该代码执行后,y 保存数组 x 中的最大值,或说 6。

3、constructor 属性

表示创建对象的函数。

object.constructor //object是对象或函数的名称。

说明:constructor 属性是所有具有 prototype 的对象的成员。它们包括除 Global 和 Math 对象以外的所有 JScript 固有对象。constructor 属性保存了对构造特定对象实例的函数的引用。

例如:

x = new String("Hi");
if (x.constructor == String) // 进行处理(条件为真)

function MyFunc {
// 函数体。
}
y = new MyFunc;
if (y.constructor == MyFunc) // 进行处理(条件为真)

以上内容就是关于本文给大家介绍的JavaScript数组去重的五种方法及javascript数组对象的三个属性,希望大家喜欢。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn