This is the 18th day of my participation in Gwen Challenge

The title

Define an array:

var arr = [1.2.3.1.1.1.NaN.NaN.undefined.undefined.null.null[], [],false.false.0.0.'1'.'2'.'1', {}, {}]/ / request to return the result is: [1, 2, 3, NaN, undefined, null, [], false, 0, "1", "2", {}]
Copy the code

Use ES6 for de-weighting

function unique1 (arr){
  return Array.from(new Set(arr))
}
// Can not remove empty object {}, empty array []
/ / output is: [1, 2, 3, NaN, undefined, null, [], [], false, 0, "1", "2", {}, {}]
Copy the code

Use indexOf to remove weights

function unique2(arr){
  let result = []
  const len = arr.length
  for(var i=0; i<len; i++){if(result.indexOf(arr[i]) === -1){
      result.push(arr[i])
    }
  }
  return result
}
// NaN, empty object, and empty array cannot be removed
/ / output is: [1, 2, 3, NaN, NaN, undefined, null, [], [], false, 0, "1", "2", {}, {}]
Copy the code

Map data structure is used for de-weighting

Create an empty Map data structure that iterates through the array to be repealed, storing each element of the array as a key in the Map. Since the Map does not have the same key value, the final result is the result after the deduplication.

function unique3(arr) {
  let map = new Map(a);let array = new Array(a);// The array is used to return the result
  for (let i = 0; i < arr.length; i++) {
    if(map.has(arr[i])) {  // If there is a key value
      map.set(arr[i], true); 
    } else { 
      map.set(arr[i], false);   // If there is no key valuearray.push(arr[i]); }}return array ;
}
// Can't get rid of empty objects and arrays
/ / output is: [1, 2, 3, NaN, undefined, null, [], [], false, 0, "1", "2", {}, {}]
Copy the code

Nested with a for loop, splice deweights

function unique4(arr){
  for(var i=0; i<arr.length; i++){for(var j=i+1; j<arr.length; j++){if(arr[j] === arr[i]){
        arr.splice(j, 1) // If the first value equals the second value, delete the second value
        j--
      }
    }
  }
  return arr
}
// NaN, empty array, and empty object cannot be removed
// [1, 2, 3, NaN, NaN, undefined, null, [], [], false, 0, "1", "2", {}, {}]
Copy the code

Use sort to remove weight

Sort the original array, loop to compare whether two adjacent elements are equal, if not, store in a new array, return a new array.

function unique5(arr){
  arr.sort()
  let result = []
  for(var i=0; i<arr.length-1; i++){
    if(arr[i+1] !== arr[i]){
      result.push(arr[i+1])}}return result
}

// NaN and empty objects cannot be removed
// [[], 0, 1, "1", 2, "2", 3, NaN, NaN, {}, {}, false, null, undefined]
Copy the code

Use includes to remove weight

Build a new array, loop through the original array, use includes to check whether the new array contains elements, if not, add the new array.

function unique6(arr) {
  var result = [];
  for (var i = 0; i < arr.length; i++) {
    if (!result.includes(arr[i])) {
      result.push(arr[i]);
    }
  }
  return result;
}
// Empty objects and arrays cannot be removed
// [1, 2, 3, NaN, undefined, null, [], [], false, 0, "1", "2", {}, {}]
Copy the code

HasOwnProperty is used for de-weighting

function unique7(arr) {
  var obj = {};
  return arr.filter(function (item, index, arr) {
    return obj.hasOwnProperty(typeof item + item)
      ? false
      : (obj[typeof item + item] = true);
  });
}
// All of them are removed
// [1, 2, 3, NaN, undefined, null, [], false, 0, "1", "2", {}]
Copy the code

Filter is used to remove weight

Determines the current element, the first index in the original array === the current index value, otherwise returns the current element

function unique8(arr) {
  return arr.filter(function(item, index, arr) {
    return arr.indexOf(item, 0) === index;
  });
}
// Unable to remove empty arrays and objects, NaN seems to be missing
// [1, 2, 3, undefined, null, [], [], false, 0, "1", "2", {}, {}]
Copy the code

Use reduce and includes to remove weight

MDN explains that the reduce() method performs a reducer function (in ascending order) provided by you on each element in the array and summarizes its results into a single return value. The reducer function receives four parameters:

  • Accumulator (ACC)
  • Current Value (CUR)
  • Current Index (IDX)
  • Source Array (SRC)

The return value from your Reducer function is assigned to the accumulator and is remembered in each iteration of the array and eventually becomes the final single result value.

function unique9(arr) {
  return arr.reduce((total, cur, index, arr) = > {
    returntotal.includes(cur) ? total : [...total, cur]; } []); }// Empty objects and arrays cannot be removed
// [1, 2, 3, NaN, undefined, null, [], [], false, 0, "1", "2", {}, {}]
Copy the code

Use ES6 Set to add… Operator de-duplication

function unique10(arr) {
  return [...new Set(arr)];
}
// [1, 2, 3, NaN, undefined, null, [], [], false, 0, "1", "2", {}, {}]
Copy the code

One last word

If this article is helpful to you, or inspired, help pay attention to it, your support is the biggest motivation I insist on writing, thank you for your support.