The example in this article describes the method of JavaScript array to weight. Share to everyone for your reference, specific as follows:
Method one.
Idea: Create a new empty array, loop through the old array, and use the indexof () method to get the element's position in the array, if a value of 1 indicates that it does not exist. So the new array uses indexof to get every element of the old array, if the value of 1 means it does not exist, push him into the new array, and finally output the new array, the heavy array.
var arr=[24,56,74,89,24,56,78,09,24];
var new_arr=[];
for (Var i=0;i<arr.length;i++) {
if (New_arr.indexof (arr[i)) ==-1) {
new_arr.push (arr[i]);
}
Console.log (New_arr);
Method Two.
var arr=[2,58,49,26,49];<br>var o={};
var new_arr=[];
for (Var i=0;i<arr.length;i++) {
var k=arr[i];
if (!o[k]) {
o[k]=true;
New_arr.push (k);
}
}
alert (New_arr);
The same principle, just borrow an empty object to judge
Note: The second method is to deposit the form of an object that has already appeared through the subscript, and the subscript reference is much faster than the indexof () search array, so the first method is very expensive.
PS: Here again for you to provide 2 similar to repeat the tool, for your reference to use:
Online removal of duplicates tool:
Http://tools.jb51.net/code/quchong
Online text to repeat tools:
Http://tools.jb51.net/aideddesign/txt_quchong
More readers interested in JavaScript-related content can view the site topics: "javascript array Operation tips Summary", "JavaScript traversal algorithm and Skills summary", "JavaScript Mathematical calculation Usage Summary", " JavaScript data structure and algorithm skills summary, "JavaScript switching effects and techniques summary", "JavaScript Search Algorithm Skills Summary", "JavaScript animation effects and techniques summary" and "JavaScript error and debugging skills Summary"
I hope this article will help you with JavaScript programming.