js---工具函数

1.base64格式图片转file文件格式

  base64toFile(dataurl, filename) {
    // 获取到base64编码
    const base64_str = dataurl.split(",");
    // 将base64编码转为字符串
    const decode_str = atob(base64_str[1]);
    let n = decode_str.length;
    // 创建初始化为0的,包含length个元素的无符号整型数组
    const u8arr = new Uint8Array(n);
    while (n--) {
      u8arr[n] = decode_str.charCodeAt(n);
    }
    return new File([u8arr], filename, {
      type: "image/png",
    });
  }

2、列表转树形结构

// 列表转树形结构
export function listToTree(list, id) {
  const arr = []
  list.forEach(item => {
  // 筛选一级部门
    if (item.pid === id) {
      // 筛选二级部门
      // 像这种自己调用自己的函数,我们叫它递归函数
      let children = listToTree(list, item.id)
         if (children.length) {
          item.children= children
           children.forEach(item => {
            listToTree(children, item.id)
    }
        }
       arr.push(item)
    }
  })
  return arr
}

猜你喜欢

转载自blog.csdn.net/h18377528386/article/details/129058219
今日推荐