ES6 Set (most commonly used in ES6)

function unique (arr) {
  return Array.from(new Set(arr))
}
var arr = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.0.0.'a'.'a', {}, {}];console.log(unique(arr))
 //[1, "true", true, 15, false, undefined, null, NaN, "NaN", 0, "a", {}, {}]
Copy the code

Regardless of compatibility, this method of de-duplication has the least code. This method also does not remove the empty ‘{}’ object; later higher-order methods add methods to remove the duplicate ‘{}’ object.

2, use for to nest for, and then splice (ES5 most commonly used)

function unique(arr){            
    for(var i=0; i<arr.length; i++){
        for(var j=i+1; j<arr.length; j++){
            if(arr[i]==arr[j]){         // The first is the same as the second, and the splice method deletes the second
                arr.splice(j,1); j--; }}}return arr;
}
var arr = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.0.0.'a'.'a', {}, {}];console.log(unique(arr))
/ / (1, "true", 15, false, undefined, NaN, NaN, "NaN", "a", {...}, {...}]
//NaN and {} are not de-duplicated
Copy the code

Double loop, outer loop elements, inner loop when comparing values. If the values are the same, delete this value.

3. Use indexOf to remove weight

function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error! ')
        return
    }
    var array = [];
    for (var i = 0; i < arr.length; i++) {
        if (array .indexOf(arr[i]) === -1) {
            array .push(arr[i])
        }
    }
    return array;
}
var arr = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.0.0.'a'.'a', {}, {}];console.log(unique(arr))
/ / (1, "true", true, 15, false, undefined, null, NaN, NaN, "NaN," 0, "a", {...}, {...}]
//NaN, {} are not de-weighted
Copy the code

Create an empty result array, loop through the original array, check whether the result array has the current element, skip if they have the same value, push into the array if they do not.

4. Use includes

function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error! ')
        return
    }
    var array =[];
    for(var i = 0; i < arr.length; i++) {
        if( !array.includes( arr[i]) ) {//includes checks whether the array has a valuearray.push(arr[i]); }}return array
}
var arr = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.0.0.'a'.'a', {}, {}];console.log(unique(arr))
/ / (1, "true", true, 15, false, undefined, null, NaN, "NaN," 0, "a", {...}, {...}]
//{} is not deduplicated
Copy the code

5. Use filter

function unique(arr) {
  return arr.filter((item, index, arr) = > {
    // Current element, the first index in the original array == current index value, otherwise return current element
    return arr.indexOf(item, 0) === index;
  });
}
var arr = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.0.0.'a'.'a', {}, {}];console.log(unique(arr))
/ / (1, "true", true, 15, false, undefined, null, "NaN," 0, "a", {...}, {...}]
Copy the code

Sort ()

function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error! ')
        return;
    }
    arr = arr.sort()
    var arrry= [arr[0]].for (var i = 1; i < arr.length; i++) {
        if(arr[i] ! == arr[i-1]) { arrry.push(arr[i]); }}return arrry;
}
var arr = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.0.0.'a'.'a', {}, {}];console.log(unique(arr))
/ / [0, 1, 15, "NaN," NaN, NaN, {...}, {...}, "a", false, null, true, "true", undefined]
//NaN, {} are not de-weighted
Copy the code

Use sort() sorting method, and then traverse and compare adjacent elements according to the sorted result.

Use hasOwnProperty

function unique(arr) {
    var obj = {};
    return arr.filter((item, index, arr) = >{
        return obj.hasOwnProperty(typeof item + item) ? false : (obj[typeof item + item] = true)})}var arr = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.0.0.'a'.'a', {}, {}];console.log(unique(arr))
/ / (1, "true", true, 15, false, undefined, null, NaN, "NaN," 0, "a", {...}]
// All the weights are gone
Copy the code

8. Use Map data structure to remove weight

function arrayNonRepeatfy(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 ;
}
var arr = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.0.0.'a'.'a', {}, {}];console.log(arrayNonRepeatfy(arr))
/ / (1, "true", true, 15, false, undefined, null, NaN, "NaN," 0, "a", {...}, {...}]
Copy the code

9. Use reduce+includes

function unique(arr){
    return arr.reduce((prev,cur) = > prev.includes(cur) ? prev : [...prev,cur],[]);
}
var arr = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.0.0.'a'.'a', {}, {}];console.log(unique(arr));
/ / (1, "true", true, 15, false, undefined, null, NaN, "NaN," 0, "a", {...}, {...}]
Copy the code

Json array deduplication

function uniqueArray(array, key){
  var result = [array[0]]
  for (var i = 1; i < array.length; i++) {
    var item = array[i]
    var repeat = false
    for (var j = 0; j < result.length; j++) {
      if (item[key] == result[j][key]) {
        repeat = true
        break}}if(! repeat) { result.push(item) } }return result[0]? result : [] }// Repeat values are accumulated
function UniquePay(paylist) {
  var payArr = [paylist[0]].for (var i = 1; i < paylist.length; i++) {
    var payItem = paylist[i];
    var repeat = false;
    for (var j = 0; j < payArr.length; j++) {
      if (payItem.chnlNo == payArr[j].chnlNo) {
        payArr[j].amount = parseFloat(payArr[j].amount) + parseFloat(payItem.amount);
        repeat = true;
        break; }}if (!repeat) {
      payArr.push(payItem);
    }
  }
  return payArr;
}
Copy the code
const paylist = [{
    chnlNo: "Cash".amount: 300.type: 2
  }, {
    chnlNo: "Alipay".amount: "100".type: 2
  },
  {
    chnlNo: "Bank card".amount: "400".type: 2
  }, {
    chnlNo: "Cash".amount: 200.type: 2
  }]
  
 // Execute
 
let arr = uniqueArray(paylist,'chnlNo')
console.log('arr', arr)
// arr [
//  { chnlNo: '现金', amount: 300, type: 2 },
// {chnlNo: 'alipay ', amount: '100', type: 2},
// {chnlNo: 'bank ', amount: '400', type: 2}
/ /]

console.log(UniquePay(paylist))
/ / /
// {chnlNo: 'cash ', amount: 500, type: 2},
//   { chnlNo: '支付宝', amount: '100', type: 2 },
// {chnlNo: 'bank ', amount: '400', type: 2}
// ]

Copy the code