JavaScript array method to analyze _javascript techniques

Source: Internet
Author: User
Tags javascript array

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.

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.