CNode

这段 js 代码有简洁的写法吗?

问答
Ggithub-amazingboy发布于8 年前最后回复8 年前15 回复4188 浏览0 收藏
var emails = [];
result.userList.map((item) => {
    emails.push(item.mailAccount);
})
$("#confirm_users").html(emails.join("<br/>"))
查看回复

回复 (15)

M
meiwhu#1·8 年前

这map用的心服口服😂

M
meiwhu#2·8 年前

result.userList.map(item => item.mailAccount).join('')

G
github-amazingboy#3·8 年前
引用 meiwhuresult.userList.map(item = item.mailAccount).join('<br / ')

@meiwhu 看了你的感觉自己好蠢

M
MiYogurt#4·8 年前

这才 fp

const prop = (id) => (obj) => obj[id]
result.userList.map(prop('mailAccount')).join(’<br />’)
Y
yviscool#5·8 年前
pipe(
  prop('userList'),
  map(prop('mailAccount')),
  join('<br />')
)
J
justjavac#6·8 年前

这是 map 啊还是 foreach 啊

M
MiYogurt#7·8 年前
  • babel-plugin-proposal-pipeline-operator
const curry = R.curry
const map = curry((fn, arr) => arr.map(fn))
const jon = curry((str, arr) => arr.join(str))
const html =  $("#confirm_users").html
result
|> prop('userlist')
|> map(prop('mailAccount'))
|> join('<br />')
|> html
const map = fn => arr => arr.map(fn)
const jon = str => arr => arr.join(str)
const html =  $("#confirm_users").html
result
|> prop('userlist')
|> map(prop('mailAccount'))
|> join('<br />')
|> html
Z
zengming00#8·8 年前
引用 MiYogurt这才 fp

@MiYogurt 不可以,这不优雅,过分使用js的动态性了,一次性代码可以这样玩,大工程绝对要禁止,反而2楼才是正确姿势

M
MiYogurt#9·8 年前

@zengming00 那深度使用 ramda 和 rxjs 的开发者怎么说。而且使用 clojure 的用户呢

Z
zengming00#10·8 年前
M
MiYogurt#11·8 年前

@zengming00 什么样的 bug,每一个函数都是单独可测试的,原子性极强,反而更利于调试才对,函数式讲究的是映射,每一个环节的函数都没有问题,那怎么会出 bug 呢?

S
Syuusuke#13·8 年前
引用 yviscool

@yviscool 我对这种写法很感兴趣, 请问这是使用的什么包的写法?

S
Syuusuke#15·8 年前
引用 yviscool@Syuusuke ramda

@yviscool 了解了, 谢谢~

参与回复
登录后即可参与回复。登录