To get rid of duplicate values in an array, the conventional solution uses some of the ARRAY apis of ES5, while a simpler approach uses a combination of the ES6 extension operator and the Set data structure.
Here are four solutions:
- Method one, use the splice method to remove duplicate elements
function normalize (arr) {
if (arr && Array.isArray(arr)) {
var len = arr.length,
map = {};
for (var i = len - 1; i >= 0; i--) {
if (arr[i] in map) {
arr.splice(i, 1);
} else {
map[arr[i]] = true; }}returnarr; }}Copy the code
- Method two uses indexOf to identify duplicate elements
function normalize (arr) {
if (arr && Array.isArray(arr)) {
var len = arr.length,
res = [];
for (var i = 0; i < len; i++) {
if (res.indexOf(arr[i]) == - 1) { res.push(arr[i]); }}returnres; }}Copy the code
- Method three, skillfully use the extension operator and Set data structure
function normalize (arr) {
if (arr && Array.isArray(arr)) {
var res = [...new Set(arr)];
returnres; }}Copy the code
- Method 4: Use the new API in ES6 —
Array.from()
To implement the function of extending the operator in method three
function normalize (arr) {
if (arr && Array.isArray(arr)) {
var res = Array.from(new Set(arr));
returnres; }}Copy the code
The code for the test is as follows:
/* Test code */
var arr = [7.1.'hi'.1.'hi'.true.null.undefined.true.null.undefined];
console.log('Array deduplicated', normalize(arr));
Copy the code