# 数组去重

# 方法1

var arr = ['aa', 'aa', 1, 1, 22, 'abc', 22];

function uniqueArr(array) {
  var _arr = [],
      isRepeat;

  for (var i = 0; i < array.length; i++) {
    isRepeat = false;

    for (var j = 0; j < _arr.length; j++) {
      if (_arr[j] == array[i]) {
        isRepeat = true;
        break;
      }
    }

    if (!isRepeat) {
      _arr.push(arr[i]);
    }

  }

  return _arr;
}

console.log(uniqueArr(arr));// ["aa", 1, 22, "abc"]

# 方法2

var arr = ['aa', 'aa', 1, 1, 22, 'abc', 22];

function uniqueArr(array) {
  var _arr = [],
    isRepeat;

  for (var i = 0; i < array.length; i++) {
    isRepeat = false;

    for (var j = i + 1; j < array.length; j++) {
      if (array[j] == array[i]) {
        isRepeat = true;
        break;
      }
    }

    if (!isRepeat) {
      _arr.push(arr[i]);
    }

  }

  return _arr;
}

console.log(uniqueArr(arr));// ["aa", 1, "abc", 22]

# 方法3

var arr = ['aa', 'aa', 1, 1, 22, 'abc', 22];

function uniqueArr(array) {
  return array.filter(function (item, index) {
    return array.indexOf(item) === index
  });

}

console.log(uniqueArr(arr));// ["aa", 1, 22, "abc"]

# 方法4

var arr = ['aa', 'aa', 1, 1, 22, 'abc', 22];

function uniqueArr(array) {
  var _arr = [];

  array.forEach(function (item) {
    if (_arr.indexOf(item) === -1) {
      _arr.push(item);
    }
  });

  return _arr;
}

console.log(uniqueArr(arr));// ["aa", 1, 22, "abc"]

# 方法5

var arr = ['aa', 'aa', 1, 1, 22, 'abc', 22];

function uniqueArr(array) {
  var _arr = [];

  array.sort();

  for (var i = 0; i < array.length; i++) {
    if (array[i] !== array[i + 1]) {
      _arr.push(array[i]);
    }
  }

  return _arr;
}

console.log(uniqueArr(arr));// [1, 22, "aa", "abc"]

# 方法6

var arr = ['aa', 'aa', 1, 1, 22, 'abc', 22];

function uniqueArr(array) {
  var _arr = [];

  array.sort();

  for (var i = 0; i < array.length; i++) {
    if (array[i] !== _arr[_arr.length - 1]) {
      _arr.push(array[i]);
    }
  }

  return _arr;
}

console.log(uniqueArr(arr));// [1, 22, "aa", "abc"]

# 方法7

var arr = ['aa', 'aa', 1, 1, 22, 'abc', 22];

function uniqueArr(array) {
  var _arr = [];

  array.forEach(function (item) {
    if (!_arr.includes(item)) {
      _arr.push(item);
    }
  })

  return _arr;
}

console.log(uniqueArr(arr));// ["aa", 1, 22, "abc"]

# 方法8

var arr = ['aa', 'aa', 1, 1, 22, 'abc', 22];

function uniqueArr(array) {
  var _arr = [];

  return array.sort().reduce(function (prev, item) {
    if (prev.length === 0 || prev[prev.length - 1] !== item) {
      prev.push(item);
    }
    return prev;
  }, []);

  return _arr;
}

console.log(uniqueArr(arr));// [1, 22, "aa", "abc"]

# 方法9

var arr = ['aa', 'aa', 1, 1, 22, 'abc', 22];

function uniqueArr(array) {
  var _arr = [],
      _temp = new Map();

  for (var i = 0; i < array.length; i++) {
    if (!_temp.get(array[i])) {
      _temp.set(array[i], 1);
      _arr.push(array[i]);
    }
  }

  return _arr;
}

console.log(uniqueArr(arr));// ["aa", 1, 22, "abc"]

# 方法10

var arr = ['aa', 'aa', 1, 1, 22, 'abc', 22];

function uniqueArr(array) {
  var _arr = [],
      _temp = {};

  for (var i = 0; i < array.length; i++) {
    if (!_temp[array[i]]) {
      _temp[array[i]] = 1;
      _arr.push(array[i]);
    }
  }

  return _arr;
}
console.log(uniqueArr(arr));// ["aa", 1, 22, "abc"]

# 方法11 推荐

var arr = ['aa', 'aa', 1, 1, 22, 'abc', 22];

function uniqueArr(array) {
  return Array.from(new Set(array));
}

console.log(uniqueArr(arr));// ["aa", 1, 22, "abc"]

# 数组扁平化

# 方法1

var arr = [[1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9], [11, 12, [12, 13, [14]]], 10];

function flatten(arr) {
  var _arr = arr || [],
      fArr = [],
      len = _arr.length,
      item;
  
  for (var i = 0; i < len; i++) {
    item = _arr[i];

    if (_isArr(item)) {
      fArr = fArr.concat(flatten(item));
    } else {
      fArr.push(item);
    }
  }

  return fArr;

  function _isArr(item) {
    return {}.toString.call(item) === '[object Array]';
  }
}

console.log(flatten(arr));// [1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12, 13, 14, 10]

# 方法2

var arr = [[1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9], [11, 12, [12, 13, [14]]], 10];

Array.prototype.flatten = function () {
  var _arr = this,
  toStr = {}.toString;

  if (toStr.call(_arr) !== '[object Array]') {
    throw new Error('只有数组才能调用flatten方法');
  }

  var fArr = [];
  
  _arr.forEach(function (item) {
    toStr.call(item) === '[object Array]'
                        ? fArr = fArr.concat(item.flatten())
                        : fArr.push(item);
  });

  return fArr;
}

console.log(arr.flatten());// [1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12, 13, 14, 10]

# 方法3

var arr = [[1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9], [11, 12, [12, 13, [14]]], 10];

Array.prototype.flatten = function () {
  var _arr = this,
  toStr = {}.toString;

  if (toStr.call(_arr) !== '[object Array]') {
    throw new Error('只有数组才能调用flatten方法');
  }

  return _arr.reduce(function (prev, item) {
    return prev.concat(
      toStr.call(item) === '[object Array]'
                          ? item.flatten()
                          : item
    );
  }, []);
}

console.log(arr.flatten());// [1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12, 13, 14, 10]

# 方法4

var arr = [[1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9], [11, 12, [12, 13, [14]]], 10];

const flatten = (arr) => {
  return arr.reduce((prev, item) => {
    return prev.concat(
      {}.toString.call(item) === '[object Array]'
                                ? flatten(item)
                                : item
    );
  }, []);
}
console.log(flatten(arr));// [1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12, 13, 14, 10]

# 方法5 推荐

var arr = [[1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9], [11, 12, [12, 13, [14]]], 10];

console.log(arr.flat(Infinity));// // [1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12, 13, 14, 10]

# 数组扁平化、去重、升序

var arr = [[1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9], [11, 12, [12, 13, [14]]], 10];

console.log(Array.from(new Set(arr.flat(Infinity))).sort((a, b) => a - b));
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

# indexOf 和 includes 区别

array.indexOf(item) -1 index 具体位置 NaN无效

array.includes(item) true false NaN有效