js方法库-持续更新

81 阅读1分钟
// 判断是否为函数

// 参数说明:
// obj: 要判断的对象

export const isFunction = (obj) => {
  return Object.prototype.toString.call(obj) === "[object Function]";
};

// 判断对象是否含有属性

// 参数说明:
// obj: 要判断的对象

export const isObjectWithAttributes = (obj) => {
  return (
    obj &&
    typeof obj === "object" &&
    !Array.isArray(obj) &&
    Object.keys(obj).length > 0
  );
};

// 判断数组是否为空

// 参数说明:
// data: 要判断的数组

export const isNonEmptyArray = (data) => {
  return Array.isArray(data) && data.length > 0;
};

// 对象数组去重

// 参数说明:
// arr: 要被去重的数组
// attr: 去重的属性名

export const uniqueArrayByKey = (arr, attr = "id") => {
  return [...new Map(arr.map((item) => [item[attr], item])).values()];
};