三十秒完成 code

距离

两点之间距离

const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0);

distance(1, 1, 2, 3); // ~2.2361

任意维度两个点的距离(欧几里得距离)

const euclideanDistance = (a, b) =>
  Math.hypot(...Object.keys(a).map((k) => b[k] - a[k]));

euclideanDistance([1, 1], [2, 3]); // ~2.2361
euclideanDistance([1, 1, 1], [2, 3, 2]); // ~2.4495

产生随机数

  1. min <= num <= max
function Random(min, max) {
  return Math.round(Math.random() * (max - min)) + min;
}

设备厘米转像素

// DPI
function getDPI() {
  var arrDPI = new Array();
  if (window.screen.deviceXDPI != undefined) {
    arrDPI[0] = window.screen.deviceXDPI;
    arrDPI[1] = window.screen.deviceYDPI;
  } else {
    var tmpNode = document.createElement("DIV");
    tmpNode.style.cssText =
      "width:1in;height:1in;position:absolute;left:0px;top:0px;z-index:99;visibility:hidden";
    document.body.appendChild(tmpNode);
    arrDPI[0] = parseInt(tmpNode.offsetWidth);
    arrDPI[1] = parseInt(tmpNode.offsetHeight);
    tmpNode.parentNode.removeChild(tmpNode);
  }
  return arrDPI;
}

// 根据厘米换算多少像素
function cmToPx(cm, dpi = getDPI()) {
  return Math.round((cm * dpi[0] ? dpi[0] : dpi) / 2.54);
}

数据的操作

从树状中获取路径(查找到所有上级节点到自己的路径)

/**
 * 根据树状的数据结构,查找符合条件的节点以及其父节点的路径
 * @param {*} tree 树状的数组
 * @param {{value: 要查找的唯一的具体值, key: 唯一标志值的key键是什么}} param1
 * @returns 类似[1,2,5,6]
 */
function findParentId(tree, { value: id, key = "id" }) {
  if (tree[key] === id) {
    return [id];
  }
  if (tree.children) {
    for (let i = 0; i < tree.children.length; i++) {
      const result = findParentId(tree.children[i], { value: id, key });
      if (result.length > 0) {
        result.unshift(tree[key]);
        return result;
      }
    }
  }
  return [];
}

在树状的数据结构中,查找符合条件的节点(树)

返回符合条件的节点(不包含上下级)

// 在树状的数据结构中,查找符合条件的节点(不包含上下级节点)
function findNode(tree, { value: id, key = "id" }) {
  if (tree[key] === id) {
    return tree;
  }
  if (tree.children) {
    for (let i = 0; i < tree.children.length; i++) {
      const result = findNode(tree.children[i], { value: id, key });
      if (result) {
        return result;
      }
    }
  }
  return null;
}

返回符合条件的节点以及其父节点的完整的树(重在过滤)

/**
 * 获取树状结构中所有符合条件的节点包含上下级树
 * @param {*} tree
 * @param {*} condition
 * @returns
 */
function filterTree(tree, condition) {
  return tree.filter((item) => {
    if (condition(item)) {
      return true;
    } else if (item.children && item.children.length) {
      item.children = this.filterDepartmentTree(item.children, val);
      return item.children.length;
    }
    return false;
  });
}

扁平化

数组扁平化

const flatten = (arr) => arr.reduce((a, v) => a.concat(v), []);
// 示例
flatten([1, [2], 3, 4]); // [1,2,3,4]

对象扁平化

const flattenObject = (obj) =>
  Object.keys(obj).reduce((acc, k) => {
    if (typeof obj[k] === "object") Object.assign(acc, flattenObject(obj[k]));
    else acc[k] = obj[k];
    return acc;
  }, {});
// 示例
flattenObject({ a: { b: { c: 1 } }, d: 1 }); // { 'a.b.c': 1, d: 1 }

平铺数组变树状结构

/**
 * 平铺数组变树状结构
 * @param {*} arr 平铺数组
 * @param {*} id 唯一标识
 * @param {*} pid 父级标识
 * @param {*} children 子级标识
 * @returns
 */
function arrayToTree(arr, id = "id", pid = "pid", children = "children") {
  let data = JSON.parse(JSON.stringify(arr));
  let result = [];
  let hash = {};
  data.forEach((item, index) => {
    hash[data[index][id]] = data[index];
  });
  data.forEach((item) => {
    let hashVP = hash[item[pid]];
    if (hashVP) {
      !hashVP[children] && (hashVP[children] = []);
      hashVP[children].push(item);
    } else {
      result.push(item);
    }
  });
  return result;
}
Last Updated: