coolpail / awesome-exercises Goto Github PK
View Code? Open in Web Editor NEW日常收集一些练习题
日常收集一些练习题
function usePrevious(value) {
const ref = useRef()
useEffect(() => {
ref.current = value
}, [value])
return ref.current
}
以下数据结构中,id 代表部门编号,name 是部门名称,parentId 是父部门编号,
为 0 代表一级部门,现在要求实现一个 convert 方法,
把原始 list 转换成树形结构,parentId 为多少就挂载在该 id 的属性 children 数组下,结构如下:
// 原始 list 如下
let list =[
{id:1,name:'部门A',parentId:0},
{id:2,name:'部门B',parentId:0},
{id:3,name:'部门C',parentId:1},
{id:4,name:'部门D',parentId:1},
{id:5,name:'部门E',parentId:2},
{id:6,name:'部门F',parentId:3},
{id:7,name:'部门G',parentId:2},
{id:8,name:'部门H',parentId:4}
];
const result = convert(list, ...);
// 转换后的结果如下
let result = [
{
id: 1,
name: '部门A',
parentId: 0,
children: [
{
id: 3,
name: '部门C',
parentId: 1,
children: [
{
id: 6,
name: '部门F',
parentId: 3
}, {
id: 16,
name: '部门L',
parentId: 3
}
]
},
{
id: 4,
name: '部门D',
parentId: 1,
children: [
{
id: 8,
name: '部门H',
parentId: 4
}
]
}
]
},
···
];
let len = list.length;
for (let i = len - 1; i >= 0; i--) {
let item = list[i];
let parentId = item.parentId;
if (list[parentId - 1]) {
if (list[parentId - 1].children) {
list[parentId - 1].children.push(list[i]);
} else list[parentId - 1].children = [list[i]];
list.splice(i, 1);
}
}
function myFlat(arr) {
let result = [];
for(let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) result = result.concat(myFlat(arr[i]))
else result = result.concat(arr[i])
}
return result;
}
function myFlat(arr) {
return arr.reduce((p, n) => p.concat(Array.isArray(n) ? myFlat(n) : n), []);
}
function lastPromise(promiseFn) {
let resetReturn;
return function () {
let cancel = false;
if (resetReturn) resetReturn();
resetReturn = () => cancel = true
return new Promise((resolve) => {
promiseFn().then((res) => !cancel && resolve(res))
})
}
}
let count = 1
let fn = () => new Promise(rs => setTimeout(() => rs(count++)))
let lastGn = lastPromise(fn)
lastGn().then(console.log)
lastGn().then(console.log)
lastGn().then(console.log)
//输入以下有序的list数组
var list = [
'h3',
'h2', 'h3',
'h1', 'h2', 'h3', 'h3', 'h2', 'h3',
'h1', 'h2', 'h4', 'h2', 'h3',s
]
//输出以下树形数组结构
[
{
name: 'h3',
child: []
},
{
name: 'h2',
child: [{name: 'h3'}]
},
{
name: 'h1',
child: [
{
name: 'h2',
child: [{name: 'h3'}, {name: 'h3'}]
},
{
name: 'h2',
child: [{name: 'h3'}]
}
]
},
{
name: 'h1',
child: [
{
name: 'h2',
child: [{name: 'h4'}]
},
{
name: 'h2',
child: [{name: 'h3'}]
}
]
}
]
let stacks = [];
function createNode(key) {
return {
name: key,
child: []
}
}
function task(node, key) {
stacks.push({
name: key,
node: node
})
}
function Compare(k1, k2) {
return Number(k1.slice(1)) <= Number(k2.slice(1))
}
function insert(el) {
let _t = stacks[stacks.length - 1];
if (!stacks.length) {
let newnode = createNode(el);
result.push(newnode);
task(newnode, el);
} else {
if (Compare(el, _t.name)) {
stacks.pop();
insert(el)
} else {
let newnode = createNode(el);
_t.node.child.push(newnode);
task(newnode, el);
}
}
}
let list = []
let total = 1000000
for(let i = 0; i < total; i++) {
list.push('h' + Math.floor(Math.random() * 10 + 1))
}
let result = []
list.forEach(insert);
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.