Skip to content

Commit 2d50235

Browse files
committed
调整
1 parent 3fb4684 commit 2d50235

File tree

2 files changed

+89
-46
lines changed

2 files changed

+89
-46
lines changed

listtree.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
let list = [
3+
{ id: 1, name: '部门A', parentId: 0 },
4+
{ id: 2, name: '部门B', parentId: 0 },
5+
{ id: 3, name: '部门C', parentId: 1 },
6+
{ id: 4, name: '部门D', parentId: 1 },
7+
{ id: 5, name: '部门E', parentId: 2 },
8+
{ id: 6, name: '部门F', parentId: 3 },
9+
{ id: 7, name: '部门G', parentId: 2 },
10+
{ id: 8, name: '部门H', parentId: 4 }
11+
];
12+
13+
14+
function convert(list) {
15+
let tree = []
16+
const map = list.reduce((res, v) => (res[v.id] = v, res), {})
17+
console.log(map)
18+
19+
20+
return tree
21+
}
22+
23+
24+
// function convert(list) {
25+
// const res = []
26+
// const map = list.reduce((res, v) => (res[v.id] = v, res), {})
27+
// for (const item of list) {
28+
// if (item.parentId === 0) {
29+
// res.push(item)
30+
// continue
31+
// }
32+
// if (item.parentId in map) {
33+
// const parent = map[item.parentId]
34+
// parent.children = parent.children || []
35+
// parent.children.push(item)
36+
// }
37+
// }
38+
// return res
39+
// }
40+
console.log(convert(list))

例题3.js

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
// 求两个数组的交集
33

4-
let arr1 = [1]
5-
let arr2 = [1, 1]
4+
let arr1 = [1,1,4]
5+
let arr2 = [1, 1,1]
66
let arr = [];
77
for (let i = 0; i < arr1.length; i++) {
88
for (let j = 0; j < arr2.length; j++) {
@@ -32,49 +32,52 @@ console.log(arr)
3232
// console.log(result)
3333

3434

35-
let mupltipy8 = (num) => num << 3
36-
console.log(mupltipy8(7))
35+
// let mupltipy8 = (num) => num << 3
36+
// console.log(mupltipy8(7))
37+
38+
// function bitAdd(m, n) {
39+
// while (m) {
40+
// [m, n] = [(m & n) << 1, m ^ n];
41+
// }
42+
// return n;
43+
// }
44+
// function multiply7(num) {
45+
// let sum = 0;
46+
// for (var i = 0; i < 7; i++) {
47+
// sum = bitAdd(sum, num);
48+
// }
49+
// return sum;
50+
51+
// }
52+
// console.log(multiply7(7)); //49
53+
54+
55+
// let str='aaadwiubcggkiwj';
56+
// const arry = str.match(/(\w)\1+/g); // 求出连续字符串
57+
// console.log(arry)
58+
// const maxLen = Math.max(...arry.map(s => s.length)); //求出最长长度
59+
// console.log(maxLen)
60+
// const result = arry.reduce((pre, curr) => {
61+
// if (curr.length === maxLen) {
62+
// pre[curr[0]] = curr.length;
63+
// }
64+
// return pre;
65+
// }, {});
66+
// console.log(result);
67+
68+
69+
// let arryn = ['1,3,2','1,89,37,4','1,4,2']
70+
// let m = arryn.toString().split(",").sort((a,b)=>{ return a-b}).map(Number)
71+
// console.log(m)
72+
// let n = [... new Set(m)] // 去重
73+
// console.log(n)
74+
75+
76+
// //parseInt() 函数解析一个字符串参数,并返回一个指定基数的整数 (数学系统的基础)。
77+
// //const intValue = parseInt(string[, radix]);
78+
// //string 要被解析的值。如果参数不是一个字符串,则将其转换为字符串(使用 ToString 抽象操作)。字符串开头的空白符将会被忽略。
79+
// //radix 一个介于2和36之间的整数(数学系统的基础),表示上述字符串的基数。默认为10。 返回值 返回一个整数或NaN
80+
// console.log(['1','2','3'].map(parseInt))
81+
3782

38-
function bitAdd(m, n) {
39-
while (m) {
40-
[m, n] = [(m & n) << 1, m ^ n];
41-
}
42-
return n;
43-
}
44-
function multiply7(num) {
45-
let sum = 0;
46-
for (var i = 0; i < 7; i++) {
47-
sum = bitAdd(sum, num);
48-
}
49-
return sum;
5083

51-
}
52-
console.log(multiply7(7)); //49
53-
54-
55-
let str='aaadwiubcggkiwj';
56-
const arry = str.match(/(\w)\1+/g); // 求出连续字符串
57-
console.log(arry)
58-
const maxLen = Math.max(...arry.map(s => s.length)); //求出最长长度
59-
console.log(maxLen)
60-
const result = arry.reduce((pre, curr) => {
61-
if (curr.length === maxLen) {
62-
pre[curr[0]] = curr.length;
63-
}
64-
return pre;
65-
}, {});
66-
console.log(result);
67-
68-
69-
let arryn = ['1,3,2','1,89,37,4','1,4,2']
70-
let m = arryn.toString().split(",").sort((a,b)=>{ return a-b}).map(Number)
71-
console.log(m)
72-
let n = [... new Set(m)] // 去重
73-
console.log(n)
74-
75-
76-
//parseInt() 函数解析一个字符串参数,并返回一个指定基数的整数 (数学系统的基础)。
77-
//const intValue = parseInt(string[, radix]);
78-
//string 要被解析的值。如果参数不是一个字符串,则将其转换为字符串(使用 ToString 抽象操作)。字符串开头的空白符将会被忽略。
79-
//radix 一个介于2和36之间的整数(数学系统的基础),表示上述字符串的基数。默认为10。 返回值 返回一个整数或NaN
80-
console.log(['1','2','3'].map(parseInt))

0 commit comments

Comments
 (0)