分享一下有赞社招笔试题

325 阅读2分钟
// 解析 URL 中的 queryString,返回一个对象 解析异常的 展示 ’‘
// 返回值示例:
// {
//   name: 'coder',
//   age: '20'.
//   callback: 'https://youzan.com?name=test',
//   list: [a, b],
//   json: {str: "abc", num: 123}, // json key 是固定
//   illegal: '', // error 异常
// }
  "https://www.youzan.com?name=coder&age=20&callback=https%3A%2F%2Fyouzan.com%3Fname%3Dtest&list[]=a&list[]=b&json=%7B%22str%22%3A%22abc%22,%22num%22%3A123%7D&illegal=C%9E5%H__a100373__b4";

function parseQueryString(url) {
  let arr = url.match(/(\?|&)[^&]*/g),
    res = {};
  arr.forEach((ele) => {
    let val = ele.slice(1).split("=");
    let key = val[0],
      value = val[1];
    if (key.includes("[]")) {
      key = key.slice(0, -2);
      value = res[key] ? [...res[key], value] : [value];
    }

    if (typeof value === "string") {
      res[key] = value
        .replace(/%2F/g, "/")
        .replace(/%3A/g, ":")
        .replace(/%3F/g, "?")
        .replace(/%3D/g, "=")
        .replace(/%7B/g, "{")
        .replace(/%22%3A/g, ":")
        .replace(/%22/g, '"')
        .replace(/%7D/g, "}");
      if ((key === "json")) {
        res[key] = JSON.parse(res[key]);
      }else{
        try {
          res[key] = decodeURI(res[key]);
        } catch (e) {
          res[key] = "";
        }
      }
      
    } else {
      res[key] = value;
    }
  });
  return res;
}
console.log(parseQueryString(testURL));
// 描述:

// 将一天24小时按每半小划分成48段,我们用一个位图表示选中的时间区间,例如`110000000000000000000000000000000000000000000000`,
// 表示第一个半小时和第二个半小时被选中了,其余时间段都没有被选中,也就是对应00:00~01:00这个时间区间。一个位图中可能有多个不连续的
// 时间区间被选中,例如`110010000000000000000000000000000000000000000000`,表示00:00-1:00和02:00-02:30这两个时间区间被选中了。

// 要求:写一个函数timeBitmapToRanges,将上述规则描述的时间位图转换成一个选中时间区间的数组。
// 示例输入:`"110010000000000000000000000000000000000000000000"`
// 示例输出:`["00:00~01:00", "02:00~02:30"]`

/**
 * 将48位的时间位图格式化成字符串
 *
 * @export
 * @param {string} bitmap 时间位图,110001010101001010000011110000111111111111111111
 * @return {Array<object>} 时间区间数组
 */
function timeBitmapToRanges(bitmap) {}

// ******************** 题目 3 ********************
/**
 * 数组去重
 *
 * @example
 * [1,'1',1]                            -> [1,'1']
 * [{a: 1}, {b: 1}, {a: 1}]             -> [{a: 1}, {b: 1}]
 * [{a: 1, b: 2}, {b: 1}, {b: 2, a: 1}] -> [{a: 1, b: 2}, {b: 1}]
 * [[1, {a: 1}], [2], [3], [1, {a: 1}]] -> [[1, {a: 1}], [2], [3]]
 */
function unique(arr) {
  let res = [],
    objVal = {};
  arr.forEach((ele) => {
    if (typeof ele === "string" || typeof ele === "number") {
      if (!res.includes(ele)) {
        res.push(ele);
      }
    } else if (ele instanceof Object) {
      if (!objVal[JSON.stringify(ele)]) {
        res.push(ele);
        objVal[JSON.stringify(ele)] = ele;
      }
    } else if (Array.isArray(ele)) {
      unique(ele);
    }
  });
  return res;
}
console.log(unique([1, "1", 1]));
console.log(unique([{ a: 1 }, { b: 1 }, { a: 1 }]));
console.log(unique([{ a: 1, b: 2 }, { b: 1 }, { a: 1, b: 2 }]));
console.log(unique([[1, { a: 1 }], [2], [3], [1, { a: 1 }]]));