Comments (6)
搭车,安利一篇我的知乎回答:JavaScript 函数式编程存在性能问题么?
from cc.
再次遇见@justjavac前辈大神
from cc.
很强!
from cc.
推荐lodash、ramda,数据结构转换超级方便,基本上不用自己写转换逻辑,理清思路找API就行了。
from cc.
哇!惊现学长@rccoder
from cc.
函数式编程reduce、map、filter、find等应用实践
// 输入为state和originData变量
// 输出为:
{
houseTypeShared: '2居_3居',
houseTypeWhole: '2居',
};
// state和originData变量如下:
// state
const state = {
sharedRooms: { 0: false, 1: true, 2:true },
wholeRooms: { 2: false, 3: true },
};
// 源数据
const originData = {
sharedRooms: [
{
unique: true,
text: '不限',
value: 'UNLIMITED',
},
{
text: '2居',
value: 'TWO',
},
{
text: '3居',
value: 'THREE',
},
{
text: '3居+',
value: 'THREE_MORE',
},
],
wholeRooms: [
{
unique: true,
text: '不限',
value: 'UNLIMITED',
},
{
text: '1居',
value: 'ONE',
},
{
text: '2居',
value: 'TWO',
},
{
text: '2居+',
value: 'TWO_MORE',
},
]
};
// 利用map, reduce, filter等函数式方法,计算出结果。
/*
好处:
1.代码精简
2.map,reduce,filter等方法更具语义化
坏处:
1.初次阅读,难以阅读理解(其实写习惯,就很容易理解了)
*/
const TypeMapParamKey = {
sharedRooms: 'houseTypeShared',
wholeRooms: 'houseTypeWhole',
};
Object.keys(state)
.map(type => {
const rtText = Object.keys(state[type])
.filter(index => state[type][index])
.map(index => originData[type][index].text)
.reduce((rt, text, index) =>
(`${rt}` + (index === 0 ? '' : '_') + `${text}`),
'',
);
return {
[TypeMapParamKey[type]]: rtText,
};
})
.reduce((rt, item) => Object.assign(rt, item), {})
from cc.
Related Issues (20)
- 48.帮学弟做题--WebSocket和Vue的五子棋 HOT 29
- 50.Material Design 风格的 Vue.js UI 组件库 beta 版本发布
- 49.理解 Vue.js 的计算属性 HOT 1
- 51.Vue.js 组件库 Rubik UI 开发心得总结 HOT 6
- 52.2016-这一年的总结和随想 HOT 3
- 53.图解 Flexbox HOT 6
- 54.Vue.js 项目使用 Karma 做自动化UI测试小结 HOT 9
- 56.图解 Flexbox 2 - 深入理解 HOT 10
- flex HOT 1
- 57.在 Vue 中使用全局共享的方式管理状态
- 58.ES2015 Proxy 实用代码示例&详解
- 59.适用于 vue.js 和原生 js 的渐进式图片加载 HOT 25
- 60.[半夜改bug] mongoose 的 createConnection 和 connect HOT 2
- 61.async/await 小技巧 HOT 4
- 62.Vue.js Little Tips HOT 6
- 63.CSS 伪类选择器
- 64.深度使用 JSON.stringify() HOT 14
- 65.从 Vue.js 自定义输入框深入理解 v-model HOT 4
- ccc HOT 3
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 cc.