Five ways to de weight an JavaScript array

  • 2020-10-07 18:34:17
  • OfStack

javascript array de-duplication is a relatively common requirement, there are many solutions, you can find the answer on the Internet, the following site to organize 1 on the same type of array de-duplication method, first give you a simple implementation idea.

Ideas:

Iterate through several groups, compare 11, compare to the same and delete the next

Iterate through the groups, 11 compare, compare to the same, skip the previous repeat, put the different into the new array

Take any one of the array elements and put it in the new array, iterate over the remaining array elements and take any one of them, compare it to the element 11 of the new array, and if there's anything different, put it in the new array.

Iterate through several groups, take 1 element as the attribute of the object, and judge 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 the normal method, it is easy to understand, if the same, 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 that the j loop continues, but the i value has changed. It's like a new i cycle:


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. Make sure 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 properties


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});;
 }

Three properties of the javascript array object

1. length attribute

The Length attribute represents the length of the array, that is, the number of elements in it. Since the index of an array always starts at 0, the upper and lower limits of an array are 0 and ES42en-1. Unlike most other languages, the length attribute of the JavaScript array is mutable, which is one point to note. When the length property is set larger, the state of the entire array does not actually change, only the length property becomes larger. When the length attribute is set smaller than the original, the values of the elements in the original array with indexes greater than or equal to length are all lost. Here is an example of changing the length attribute:

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

// defines an array of 10 Numbers

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

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

alert(arr.length); // displays that the length of the array has changed to 12

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

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

alert(arr[8]); // Shows that the 9th element has been changed to "undefined"

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

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

From the above code we can clearly see the nature of the length attribute. But not only can the length object be explicitly set, it can also be implicitly modified. You can use an undeclared variable in JavaScript, or you can use an undefined array element with an index greater than or equal to length, where the value of the length attribute is set to the value of the element index used plus 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 of 10 Numbers, with length 10 as shown by the alert statement. An element with an index of 15 is then used and assigned a value of 15, that is, arr[15]=34. Then the alert statement is used to output the length of the array, and the result is 16. However, this is a very surprising feature for developers used to strongly typed programming. In fact, the initial length of an array created in the form of new Array() is zero, and it is the manipulation of undefined elements in the array that changes the length of the array.

As you can see from the above introduction, the length attribute is so magical that you can easily increase or decrease the size of an array. Therefore, a deep understanding of length attributes is conducive to flexible application in the development process.

2. prototype attribute

Returns a reference to the object type stereotype. The prototype attribute is common to object.

objectName.prototype

The objectName parameter is the name of the object object.

Description: Use the prototype attribute to provide a basic set of functions for the class of the object. A new instance of an object "inherits" the operation given to the object prototype.

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

Adds a method to an array object that returns the maximum element value in the array. To do 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( );

After this code executes, y holds the maximum value in the array x, or 6.

3. constructor attribute

Represents the function that creates the object.

object. constructor //object is the name of an object or function.

Note: the constructor attribute is a member of all objects that have prototype. They include all JScript native objects except Global and Math objects. The constructor attribute holds a reference to a function that constructs an instance of a particular object.

Such as:


x = new String("Hi");
if (x.constructor == String) //  Process (condition true) 

or


function MyFunc {
//  The body of the function. 
}
y = new MyFunc;
if (y.constructor == MyFunc) //  Process (condition true) 

The above content is about the JavaScript array to introduce to you 5 methods and javascript array object 3 attributes, I hope you like.


Related articles: