每日一题

2021-03-10 隐私协议替换文案

const result = "我已阅读并同意 %{var}, %{var2} 隐私协议1和隐私协议2".replace(/(?=\%{).+?(?<=\})/g, (key, start, string) => {
    return '$' + key + '$';
});
console.log(result);

let array = result.split("$").filter(it => it.length > 0);
console.log(array);


array = array.map(it => {
	const item = {};
	item.value = it;
	item.color = 'gray';
	item.link = "";
	if(it === "%{var}") {
		item.link = "http://baidu.com";
		item.color = "red";
		item.value = "隐私协议1";
	}
	if( it === "%{var2}") {
		item.link = "http://google.com";
		item.color = "red";
		item.value = "隐私协议2";
	}
	return item;
});
console.log(array);

2021-03-11 微信红包算法

function getWechatRedPacket(totalMoney, totalPeople) {
	let minMoney = 0.01;
	let maxMoney;
	let currentMoney;
	let sumList = [];
	while(totalPeople > 1) {
		maxMoney = totalMoney * 2.0 / totalPeople;
		currentMoney = Math.random() * maxMoney;
		currentMoney = currentMoney < 0.01 ? 0.01 : currentMoney;
        console.log(currentMoney);
		currentMoney = Math.round(100 * currentMoney)/100;
		--totalPeople;
		totalMoney -= currentMoney;
		sumList.push(currentMoney);
	}

	sumList.push(Math.round(totalMoney * 100)/ 100);
	return sumList;
}

2021-03-11通过可选链深度设置对象的值

方案一: 通过reduce去遍历
function getObj(obj = {a:{b:{c:1}}},path = 'a.b.c', value = 2) {
   let arr = path.split('.');
   let newObj = {...obj};
   arr.reduce((preValue, it, index) => {
        const flag = !preValue || !preValue[it];
        if(flag) {
          newObj = obj;
          return preValue;
        }
        index === (arr.length - 1) && preValue[it] && (preValue[it] = value);
        return preValue[it];
    }, newObj);
    return newObj;
}

2021-03-12 - 字符串中字符出现的次数

	function getRepeatKeyWithString(str = '') {
		let map = {};
		if(!str || typeof str !== 'string') {
			return [];
		}
		// 这里不考虑数组
		str.split('').forEach(key => {
			map[key] = -~map[key];
		});
		console.log(map);
	}

2021-03-17 - 排序

// 1.快速排序
function quicklySort(array = []) {
  if (array?.length <= 1) {
    return array;
  }

  const centerIndex = Math.floor(array.length / 2);
  const centerItem = array.splice(centerIndex, 1);
  let leftList = [];
  let rightList = [];
  array.forEach((it) => {
    if (it < centerItem) {
      leftList.push(it);
    } else {
      rightList.push(it);
    }
  });
  return quicklySort(leftList).concat(centerItem, quicklySort(rightList));
}

const list = quicklySort([1, 9, 234, 432, 12, 9, 2, 98, 34]);
// 1.冒泡排序
function bubblingSort(array = []) {
  if (!array?.length) {
    return array;
  }

  const length = array.length;
  for (let i = 0; i < length - 1; i++) {
    for (let j = 0; j < length - i - 1; j++) {
      if (array[j + 1] < array[j]) {
        [array[j], array[j + 1]] = [array[j + 1], array[j]];
      }
    }
  }
  return array;
}

const list = bubblingSort([1, 9, 234, 432, 12, 9, 2, 98, 34]);
console.log("li:", list);
// 3.flat实现
const arr = [1, [2, [3, 4]],[[[1,2]]]];
function flatten(arr) {
  while (arr.some((item) => Array.isArray(item))) {
    console.log("arr:", arr);
    arr = [].concat(...arr);
  }
  return arr;
}

console.log(flatten(arr));

2021-04-29 – 6种数组拷贝的方法

Array.slice(arr);
[...arr];
Array.map(it => it);
Array.from(arr);
Object.assign([], arr);
arr.filter(it => it);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值