Comments (2)
经常用这个方法获取一张图片的图形数。
通过让canvas读取图片获取图片像素矩阵,从而获取图形个数。
from blog.
const list = [{
id: 1,
name: '江苏',
children: [{
id: 11,
name: '南京',
children: [{
id: 111,
name: '栖霞区'
}]
}]
},
{
id: 2,
name: '山东',
children: [{
id: 21,
name: '济南',
children: [{
id: 211,
name: '历下区'
},
{
id: 212,
name: '城中区'
}]
}]
}];
// 实现一个方法,可以展开该数组[{id: 1, name: '江苏'}, {id: 11, name: '南京'}, ...]
// reduce解法
function flatten (array) {
return array.reduce(
(sum, item) => {
sum.push({
id: item.id,
name: item.name
})
return Array.isArray(item.children) ? sum.concat(flatten(item.children)) : sum.concat([])
}, []
)
}
// dfs解法
function flatten (array) {
const list = []
let item
while(item = array.shift()) {
list.push({
id: item.id,
name: item.name
})
if (item.children) {
array = item.children.concat(array)
}
}
return list
}
// bfs解法
function flatten(array) {
const list = []
while(item = array.shift()) {
list.push({
id: item.id,
name: item.name
})
if (item.children) {
array = array.concat(item.children)
}
}
return list
}
// 普通递归做法
function flatten (array) {
if (!array.length) return []
let list = []
while (item = array.shift()) {
list.push({
id: item.id,
name: item.name
})
if (item.children) {
list = list.concat(flatten(item.children))
}
}
return list
}
const aa = flatten(list)
console.log(aa)
from blog.
Related Issues (20)
- 毁三观js
- Vue slot的使用。 HOT 2
- 最后的整合,Vue与快应用发送指令策略。
- Vue v-model 指令的移植
- vue的一些使用规范,自我体会版。
- Vuex 使用心得
- go context包的使用心得
- typescript的各种import方式 HOT 2
- react HOC ref的使用。 HOT 2
- 快应用平台引入Vue框架
- reactComponent和reactElement的区别 HOT 1
- 实现promise
- js回调的一些使用 HOT 7
- leetcode 42
- addEventListener的option选项 HOT 1
- 设计一个并发为2的调度器 HOT 2
- 精读immer系列 HOT 3
- 设计模式记录---模板方法 HOT 1
- 设计模式记录---单例模式 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.