In es5, you can use the for statement and indexOf() function to achieve array deduplication. The syntax is "for(i=0;i
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
var arr = [1,2,3,1,3,4,5]; Array.prototype.myInfo = function(){ var newArr = []; for(var i=0;i<arr.length var if newarr return console.log><p><img src="https://img.php.cn/upload/article/000/000/024/356e180a6b80e6d664a8d03b344e4adc-0.png" alt="How to implement array deduplication in es5 and es6"></p> <h2> <a id="es6__21"></a>es6 Array deduplication<strong></strong> </h2> <p><a id="1_Arrayfrom__Set_22"></a><strong>1. Array.from and Set<span style="font-size: 16px;"></span></strong></p>Set does not allow duplicate elements<p></p> <pre class="brush:php;toolbar:false"> let arr = [1,2,1,2,3]; let result = new Set(arr); console.log(result); console.log(Array.from(result));
## 2. rest and Setrest method operator is "
…"
… Strips characters from the array let arr = [1,2,1,2,3];
let result = new Set(arr);
console.log(result);
console.log(...result);
console.log([...result]);
console.log([...new Set(arr)]);
【Related recommendations:
The above is the detailed content of How to implement array deduplication in es5 and es6. For more information, please follow other related articles on the PHP Chinese website!