⭐ 写一个提取 URL 参数的 js 方法

例如:url="[<https://alibaba.com?a=1&b=2&c=3#hash>"](<https://alibaba.com/?a=1&b=2&c=3#hash%22>)

要求:识别 queryParam

let url = "<https://alibaba.com?a=1&b=2&c=3#hash>"
function getParamFromURL(url) {
 //...your code
}
const url = "<https://alibaba.com?a=1&b=2&c=3#hash>";
function getParamFromURL(url){ //定义函数
  var pattern = /(\\w+)=(\\w+)/ig;//定义正则表达式
  var params = {};//定义数组
  url.replace(pattern, function(a, b, c) {
    console.log(a, b, c); // a=1 a 1; b=2 b 2; c=3 c 3
    params[b] = c;
  });
  return params;//返回这个对象.
}
console.log(getParamFromURL(url));

或者

const url = "<https://alibaba.com?a=1&b=2&c=3#hash>";
function getParamFromURL(url){ //定义函数
  const reg = /(\\w+)=(\\w)/ig;
  const arr = url.match(reg) ?? []; // 防止为 null | [ 'a=1', 'b=2', 'c=3' ]
  const obj = {};
  for (const item of arr) {
    const [key, value] = item.split('=');
    obj[key] = value;
  }
  return obj;
}
console.log(getParamFromURL(url));

或者原生 API

const url = "<https://alibaba.com?a=1&b=2&c=3#hash>";
function queryURLParams(URL) {
  let url = URL.split("?")[1];
  const urlSearchParams = new URLSearchParams(url);
  const params = Object.fromEntries(urlSearchParams.entries());
  return params
}
console.log(queryURLParams(url))

如何将浮点数点左边的数每三位添加一个逗号,如 12000000.11 转化为『12,000,000.11』?

// 从后往前,每三个为一次循环,最前面加,
// ,000
// ,000,000
// 12,000,000
function toThousands(num) {
    if (typeof num !== 'number') return 0;
    var flag = "";
    if (num < 0) {
        flag = "-";
        num = Math.abs(num);
    }
    // 转为数组 [ '12000000', '11' ]
    var arr = num.toString().split(".");
    // 截取.左边和右边存起来
    var left = [...arr[0]];
    var right = "";
    if (arr.length > 1) {
        right = "." + arr[1];
    }
    var count = left.length - 1;
    // 操作左边整数部分,逆向遍历并且逢3前面加个"," i-1
    while (count > 0) {
        // [1,2,0,0,0,0]
        // 例如如果数组长度为6,则一开始count=5,不加,
        // count=3时,就需要在前面加个,
        if (count % 3 === 0) {
            left.splice(-count, 0, ',');
        }
        count--;
    }
    // 正负符号+左边+小数点和右边
    return flag + left.join("") + right;
}
// 正则
function toThousands(num) {
  return num && num
    .toString()
    .replace(/(\\d)(?=(\\d{3})+\\.)/g, function($1, $2){
    return $2 + ',';
  });
}
// toLocaleString

// 利用 toLocaleString() 返回某语言系统下数字的表示字符串 IE6+
var num = 12000000.11
num.toLocaleString()

字符串去重

var str = "111222333000aaa我我我";

String.prototype.myUnique = function() {
	var hash = {},
      newStr = "";
  
  for (var i = 0; i < this.length; i++) {
  	if (!hash.hasOwnProperty(this[i])) {
    	hash[this[i]] = this[i];
      newStr += this[i];
    }
  }
  
  return newStr;
};

console.log(str.myUnique());
// 或者
 String.prototype.myUnique = function() {
	 return Array.from(new Set(this.split('')));
 }

找出一串字符串中所有只出现一次的字符的索引组成的数组

var str = "saso1idangvcas3gkxkxlaknzgegunclas";

function test(str) {
	var hash = {},
      newArr = [];
  
  for (var i = 0; i < str.length; i++) {
    var item = str[i];
  	if (!hash.hasOwnProperty(item)) {
    	hash[item] = 1;
    } else {
    	hash[item]++;
    }
  }
  
  console.log(hash);
  
  for (var i = 0; i < str.length; i++) {
  	if (hash[str[i]] === 1) {
      console.log(str[i]);
    	newArr.push(i);
    }
  }
  
  return newArr;
}

console.log(test(str));

或者

var str = "saso1idangvcas3gkxkxlaknzgegunclas";

function test(str) {
  let arr = [];

  str.split("").forEach((char) => {
    const firstIdx = str.indexOf(char);
    const lastIdx = str.lastIndexOf(char);
    if (firstIdx === lastIdx) {
      arr.push(firstIdx);
    }
  });

  return arr;
}

console.log(test(str));