a.数组去重

1.单个数组去重

  1. //1.
  2. function unique(arr) {
  3. return [...new Set(arr)]
  4. }
  5. //2.
  6. function unique(arr) {
  7. return Array.from(new Set(arr))
  8. }
  9. //3.
  10. function unique(arr) {
  11. var arrs = [];
  12. for (var i = 0; i < arr.length; i++) {
  13. if (arrs.indexOf(arr[i]) == -1) {
  14. arrs.push(arr[i])
  15. }
  16. }
  17. return arrs;
  18. }

2.两个数组合并去重

  1. //1.
  2. function unique(arr1, arr2) {
  3. return Array.from(new Set([...arr1, ...arr2]))
  4. }
  5. //2.
  6. function unique(a, b) {
  7. let arr = a.concat(b)
  8. arr = arr.sort(function (a, b) {
  9. return a - b;
  10. });
  11. let result = [arr[0]]
  12. for (let i = 1, len = arr.length; i < len; i++) {
  13. arr[i] !== arr[i - 1] && result.push(arr[i])
  14. }
  15. return result
  16. }

b.数组对象去重,数组中元素为对象,去除相同key值的对象

  1. //1.
  2. function unique(arr, keyName) {
  3. let obj = {};
  4. return arr.reduce((cur, next) => {
  5. obj[next[keyName]] ? "" : obj[next[keyName]] = true && cur.push(next);
  6. return cur;
  7. }, [])
  8. }
  9. //2.
  10. function unique(oldArr,keyName) {
  11. var allArr = [];
  12. for (var i = 0; i < oldArr.length; i++) {
  13. var flag = true;
  14. for (var j = 0; j < allArr.length; j++) {
  15. if (oldArr[i][keyName] == allArr[j][keyName]) {
  16. flag = false;
  17. };
  18. };
  19. if (flag) {
  20. allArr.push(oldArr[i]);
  21. };
  22. };
  23. return allArr
  24. }
  25. //去重
  26. deWeight(arr) {
  27. for (var i = 0; i < arr.length - 1; i++) {
  28. for (var j = i + 1; j < arr.length; j++) {
  29. if (arr[i].id == arr[j].id) {
  30. arr.splice(j, 1);
  31. //因为数组长度减小1,所以直接 j++ 会漏掉一个元素,所以要 j--
  32. j--;
  33. }
  34. }
  35. }
  36. return arr;
  37. },

c.判断各种数据类型及无效值

1.判断是否是一个数组

  1. function isArray(arr){
  2. return Object.prototype.toString.call(arr) ==='[object Array]';
  3. //return arr.constructor === Array;
  4. }
  5. isArray([1,2,3]) //true

2.判断是否是一个函数

  1. function isFunction(fn) {
  2. return Object.prototype.toString.call(fn) ==='[object Function]';
  3. //return fn.constructor === Function;
  4. //return fn instanceof Function;
  5. //return typeof fn === "function";
  6. }

3.判断是否是一个对象

  1. function isObject(obj) {
  2. retrun Object.prototype.toString.call(obj) ==='[object Object]';
  3. //return obj.constructor === Object;
  4. //return obj instanceof Object;
  5. //return typeof obj === "object";
  6. }

4.判断是否是一个字符串

  1. function isString(str) {
  2. retrun Object.prototype.toString.call(str) ==='[object String]';
  3. //return str.constructor === String;
  4. //return typeof str === "string";
  5. }

5.判断是否是一个数字

  1. function isNumber(num) {
  2. retrun Object.prototype.toString.call(num) ==='[object Number]';
  3. //return num.constructor === Number;
  4. //return typeof num=== "number";
  5. }

6.判断是否是一个时间对象

  1. function isDate(date) {
  2. retrun Object.prototype.toString.call(date) === "[object Date]";
  3. //return date.constructor === Date;
  4. }

7.判断是否是一个布尔值

  1. function isBoolean(bool) {
  2. retrun Object.prototype.toString.call(bool) ==='[object Boolean]';
  3. //return bool.constructor === Boolean;
  4. //return typeof bool=== "boolean";
  5. }

8.判断是否是一个空、无效的值(null、undefined、””、[]、{}、NaN)

  1. function isVoid(obj) {
  2. if (obj == null || obj == undefined || obj == "") {
  3. return true
  4. } else if (Object.prototype.toString.call(obj) === '[object Array]' && obj.length === 0) {
  5. return true
  6. } else if (Object.prototype.toString.call(obj) === '[object Object]' && Object.keys(obj).length === 0) {
  7. return true
  8. }else if(isNaN(obj)){
  9. return true
  10. }
  11. return false
  12. }