前端小方法

前端二进制流文件下载
/*
   *@param data:二进制流文件
   *@param fileName:文件名称
   */
  const downLoadExcel = (data, fileName = "") => {
    const fileName = `${fileName}.xlsx`;
    const blob = new Blob([data], { type: "application/vnd.ms-excel" });
    if (typeof window.navigator.msSaveBlob !== "undefined") {
      window.navigator.msSaveBlob(blob, fileName);
    } else {
      const blobURL = window.URL.createObjectURL(blob);
      const tempLink = document.createElement("a");
      tempLink.style.display = "none";
      tempLink.href = blobURL;
      tempLink.setAttribute("download", fileName);
      if (typeof tempLink.download === "undefined") {
        tempLink.setAttribute("target", "_blank");
      } else {
        document.body.appendChild(tempLink);
        tempLink.click();
        document.body.removeChild(tempLink);
        window.URL.remokeObjectURL(blobURL);
      }
    }
  };
获取url上参数
const getUrlParams=(url,keyName)=>{
    const splitUrl = url.split('?')[1]?.split('&')
    const obj = {}
    splitUrl.forEach(item => {
        const [code,value] = item.split('=')
        obj[code]=value
    });
    return obj[keyName]
}
嵌套关系的数组根据子元素的id获取所有父元素的某个特定属性值的集合
 const getParentCustomKeyValueArr = (
        data = [],
        nodeFiledValue,
        customKey = "id",
        fieldName = "id",
        parentFieldName = "parentId",
        childrenName = "children"
      ) => {
        let arrRes = [];
        if (data.length == 0) {
          if (!!nodeFiledValue && fieldName == customKey) {
            arrRes.unshift(data);
          }
          return arrRes;
        }
        const fn = (data2 = [], nodeFiledValue2) => {
          for (let index = 0; index < data2.length; index++) {
            const node = data2[index];
            if (node[fieldName] == nodeFiledValue2) {
              arrRes.unshift(node[customKey]);
              fn(data, node[parentFieldName]);
              break;
            } else {
              if (!!node[childrenName]) {
                fn(node[childrenName], nodeFiledValue2);
              }
            }
          }
          return arrRes;
        };
        arrRes = fn(data, nodeFiledValue);
        return arrRes;
      };

例如

const temp = [
        {
          label: "天下第一",
          id: 1,
          children: [
            {
              label: '天下第一子',
              id: 11,
              parentId: 1,
              children: [
                {
                  label: '天下第一孙',
                  id: 111,
                  parentId: 11,
                },
              ],
            },
            {
              label: '天下第二',
              id: 12,
              parentId: 1,
              children: [
                {
                  label: '天下第二-子',
                  id: 121,
                  parentId: 12,
                },
              ],
            },
          ],
        },
        {
          label: '天下第二',
          id: 2,
          children: [
            {
              label: 21,
              id: 21,
              parentId: 2,
              children: [
                {
                  label: 211,
                  id: 211,
                  parentId: 21,
                },
              ],
            },
          ],
        },
      ];
      getParentCustomKeyValueArr(temp, "111", "label")
     //得到 ['天下第一', '天下第一子', '天下第一孙']
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值