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.