美文网首页
Algo| Codes| getBySelector| form

Algo| Codes| getBySelector| form

作者: 玫瑰的lover | 来源:发表于2024-10-27 13:23 被阅读0次

getBySelector

export const obj = {
  selector: { to: { toutiao: "FE coder" } },
  target: [1, 2, { name: "byted" }],
};

const getBySelector = (...args) => {
  const source = args[0];
  const selectors = args.slice(1);
  const res: any[] = [];
  selectors.forEach((s) => {
    const arr = s.split(/\.|\[|\]/g);
    const notEmptyArr = arr.filter((v) => v);
    const v = notEmptyArr.reduce((prev, item) => {
      return prev?.[item];
    }, source);
    res.push(v);
  });
  return res;
};
getBySelector(obj, "selector.to.toutiao", "target[0]", "target[2].name");

formatWithComma


const test: string = "1234567890";
const formatWithComma = (s: string) => {
  if (s.length < 4) {
    return s;
  }
  let res = "";
  const seperator_char = ",";
  const seperator_space = 3;
  let count = 0;
  for (let i = s.length - 1; i >= 0; i--) {
    count++;
    res = s[i] + res;
    if (count === seperator_space) {
      res = seperator_char + res;
      count = 0;
    }
  }
  return res;
};
console.log(formatWithComma(test), "Test utils is running");

mergeSortedArray

export const mergeSortedArr = (a: Array<number>, b: Array<number>) => {
  if (a.length < 1) {
    return b;
  }
  if (b.length < 1) {
    return a;
  }
  try {
    const merged = [];
    let i = 0;
    let j = 0;
    while (a[i] && b[j]) {
      if (a[i] <= b[j]) {
        merged.push(a[i]);
        i++;
      } else {
        merged.push(b[i]);
        j++;
      }
    }
    while (i < a.length) {
      merged.push(a[i]);
      i++;
    }
    while (j < b.length) {
      merged.push(b[j]);
      j++;
    }
    return merged;
  } catch (e) {
    console.log(`===${e}===`);
  }
};

相关文章

  • Strategy Pattern

    IntentDefine a family of algorithms(algo1, algo2, ...);En...

  • windows batch runs in background

    add some codes in the before of your codes link

  • 2019-06-15

    kv is data step is algo what about view? view(kv,algo) vi...

  • CFNetwork错误代码引用

    All error codes are on "CFNetwork Errors Codes References...

  • algo

    夕阳也许坠毁 天空对大地呼喊 我不能够 我不能够 灰黑的暗礁 潮水涌动地扑朔迷离 多年以前的她 转身 黑夜里 看见...

  • Algo

    图灵奖级别资金盘的正确姿势- 好多人到现在还没有搞清楚Algorand设计的资金盘有趣之处。 资金盘在中文里貌似有...

  • Regex validate PIN code(codewars

    ATM machines allow 4 or 6 digit PIN codes and PIN codes c...

  • NVIDIA Samples

    0. Build Nvidia Sample Codes Make CUDA sample codes for d...

  • scrapy遇到403 500 502等状态码时的应对

    遇到错误忽略掉不重新请求,则设成[] RETRY_HTTP_CODES = [] RETRY_HTTP_CODES...

  • 聊algo

    algo(计算) -> 会关注计算的步骤数 -> 不同的结构会影响步数 线性表(数组,链表) 树(树,二叉树) ...

网友评论

      本文标题:Algo| Codes| getBySelector| form

      本文链接:https://www.haomeiwen.com/subject/ilyprjtx.html