A method for JavaScript to remove repeating elements in an array _javascript tips

Source: Internet
Author: User

The example in this article describes the method of repeating elements within a JavaScript-shifted divisor group. Share to everyone for your reference. The specific analysis is as follows:

This JS code is used to remove duplicate elements from the array, such as: [' apple ', ' orange ', ' peach ', ' apple ', ' strawberry ', ' orange '] go back after heavy: s [' Apple ', ' orange ', ' Peach ', ' strawberry ']

Copy Code code as follows:
function RemoveDuplicates (arr) {
var temp = {};
for (var i = 0; i < arr.length; i++)
Temp[arr[i]] = true;
var r = [];
For (var k in temp)
R.push (k);
return R;
}
Usage
var fruits = [' apple ', ' orange ', ' peach ', ' apple ', ' strawberry ', ' orange '];
var uniquefruits = removeduplicates (fruits);
Print uniquefruits [' Apple ', ' orange ', ' peach ', ' strawberry '];

The following code can be validated in the browser

Copy Code code as follows:
Remove duplicate elements from a array. <br>
<pre> var fruits = [' apple ', ' orange ', ' peach ', ' apple ', ' strawberry ', ' orange '];
</pre>
Note ' Orange ' are duplicate in fruits array. Click to remove duplicate elements from fruits array:<br>
<button onclick= "Check ()" >remove duplicate</button>
<script>
function RemoveDuplicates (arr) {
var temp = {};
for (var i = 0; i < arr.length; i++)
Temp[arr[i]] = true;
var r = [];
For (var k in temp)
R.push (k);
return R;
}
function Check () {
var fruits = [' apple ', ' orange ', ' peach ', ' apple ', ' strawberry ', ' orange '];
var uniquefruits = removeduplicates (fruits);
alert (uniquefruits);
}
</script>

I hope this article will help you with your JavaScript programming.

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.