常用小算法

排序

// 排序从小到大
let a = [5,3,12,16,2,35,4]
a.forEach((i,index) => {
	a.forEach((j,index1) => {
		if (index1 > index && a[index] > a[index1]) {
		  let v = a[index]
		  let x = a[index1]
		  a[index] = x;
		  a[index1] = v;
		}
	})
});
console.log(a);
// 输出 [2, 3, 4, 5, 12, 16, 35]
// 从大到小
a.forEach((i,index) => {
	a.forEach((j,index1) => {
		if (index1 > index && a[index1] > a[index]) {
		  let v = a[index]
		  let x = a[index1]
		  a[index] = x;
		  a[index1] = v;
		}
	});
});
console.log(a);
// 输出[35, 16, 12, 5, 4, 3, 2]
思路:已 5,3,12,16,2,35,4 为例 第一个和第二个比较 如果第一个大于二个 那么交换位置 然后第一个在和第三个比较 以此类推比较。 一轮下来最小的就会排在最前头。第二轮在那第二个和第三个比较....。第二轮就会把第二小的排在第二位...(index1 > index 判断了只会和下一个进行比较)

树转换

  1. 第一种
const list = [{"id": 1, "name": "一级"},{"id": 2, "name": "二级", "pid": 1},{"id": 3, "name": "三级", "pid": 2},
{"id": 4, "name": "二级", "pid": 1},{"id": 5, "name": "一级01"},{"id": 6, "name": "二级", "pid": 5},{"id": 7, "name": "三级01", "pid": 2}]
const listTree = []
recursion = (arr,data) => {
	list.forEach((json,i) => {
		if (json.pid === data.id) {
				data.list.push(json);
				json.list=[];
				recursion(list,json);
				
		} 
	 })
}
list.forEach((json,i) => {
		if (!json.pid) {
			json.list = [];
			listTree.push(json);
			recursion(list,json)
			
		} 
})
  1. 第二种
const list = [{"id": 1, "name": "一级"},{"id": 2, "name": "二级", "pid": 1},{"id": 3, "name": "三级", "pid": 2},
{"id": 4, "name": "二级", "pid": 1},{"id": 5, "name": "一级01"},{"id": 6, "name": "二级", "pid": 5},{"id": 7, "name": "三级01", "pid": 2}]
recursion = (arr, pid)=> {
	const list1 = [];
	 arr.forEach((json,i) => {
		if (json.pid === pid) {
				list1.push(json);
				const list2=recursion(list,json.id);
				if (list2.length > 0) {
					json.list = list2;
				}
		} 
	 });
	 return list1;
}
const listTree = recursion(list)

合计次数并通过出现的次数排序

// 合计次数并通过出现的次数排序
let a = [5,3,12,16,2,35,4,5,6,3,4,6,7,8,6,3,4,5,6,7,9,1,2,4,5,8,3,4,6,7,8,5,4,3,2,9];
// 作为出现的次数json组,格式[{"number": 3, "frequency": 4}.......]
const list = [];
//先计算出每个数字出现的次数
a.forEach((number) => {
	let on = true;
	list.forEach((json) => {
		if (number === json.number ) {
			json.frequency = json.frequency +1;
			on = false;
		} 
	});
	if (on) {
		const json = {"number": number, "frequency": 1};
		list.push(json);
	}
});
console.log(list);
// 根据frequency进行正序排序
list.forEach((json,index) => {
	list.forEach((json1,index1) => {
		if (index1 > index && list[index].frequency > list[index1].frequency) {
		  let v = list[index1]
		  let x = list[index]
		  list[index] = v;
		  list[index1] = x;
		}
	})
});
console.log(list);
// 根据frequency进行倒叙序排序
list.forEach((json,index) => {
	list.forEach((json1,index1) => {
		if (index1 > index && list[index].frequency < list[index1].frequency) {
		  let v = list[index1]
		  let x = list[index]
		  list[index] = v;
		  list[index1] = x;
		}
	})
});
console.log(list);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值