function deduplicatObj(arr: Array<any>) {
  const cloneArr = JSON.parse(JSON.stringify(arr));
  const res = cloneArr.map((item: any) => {
    const types = Object.prototype.toString.call(item);
    if (types === '[object Array]') {
      return handleArr(item);
    } else if (types === '[object Object]') {
      return handleObj(item);
    } else {
      return item;
    }
  });
  return res;
}

function handleArr(data: Array<any>) {
  return JSON.parse(JSON.stringify(deduplicatObj(data))).sort();
}

function handleObj(data: any) {
  for (const key in data) {
    if (Object.prototype.hasOwnProperty.call(data, key)) {
      const element = data[key];
      const types = Object.prototype.toString.call(element);

      if (types === '[object Array]') {
        data[key] = handleArr(element);
      } else if (types === '[object Object]') {
        data[key] = handleObj(element);
      } else {
        //
      }
    }
  }

  // 把对象按照默认顺序排序
  const keyslist = Object.keys(data).sort();
  const myobj = {} as any;
  keyslist.map((item: string) => {
    myobj[item] = data[item];
  });

  return myobj;
}

/**
 * 数组对象去重 返回一个新数组
 * @param data - 原始数组
 **/
export const noRepeatObjInArr = function (data: Array<any>) {
  const res = deduplicatObj(data);
  const mySet: Set<string> = new Set();
  res.map((item: any) => {
    const JSONstr = JSON.stringify(item);
    mySet.add('JSON_' + JSONstr);
  });

  const arr = [] as any[];
  for (const item of mySet) {
    const itemArr = item.split('JSON_', 2);
    arr.push(JSON.parse(itemArr[1]));
  }
  return arr;
};
