内容简介:若要抽取 Object 的部分 Property,Ramda 提供了VS Code 1.31.1Quokka 1.0.136
若要抽取 Object 的部分 Property,Ramda 提供了 pick()
;但若要抽取的是大部分 Property,可從反面思考,僅 忽略
部分的 Property,這就是 omit()
。
Version
VS Code 1.31.1
Quokka 1.0.136
Ramda 0.26.1
pick()
import { map, pick } from 'ramda'; const data = [ { id: 1, title: 'Functional Programming in JavaScript', year: 2016 }, { id: 2, title: 'RxJS in Action', year: 2017 }, { id: 3, title: 'Speaking JavaScript', year: 2014 } ]; const getBooks = map(pick(['title', 'year'])); console.dir(getBooks(data));
由 pick()
傳入想要抽取的 property array,會回傳 map()
所需的 projection function。
使用 pick()
雖然直覺,但若大部分的 property 都要抽取,則 property array 會非常大。
pick()
[k] -> {k: v} -> {k: v}
從一個 object 中抽取部分 property 成為新 object
omit()
import { map, omit } from 'ramda'; const data = [ { id: 1, title: 'Functional Programming in JavaScript', year: 2016 }, { id: 2, title: 'RxJS in Action', year: 2017 }, { id: 3, title: 'Speaking JavaScript', year: 2014 } ]; const getBooks = map(omit(['id'])); console.dir(getBooks(data));
omit()
為 pick()
的 complement function,僅需提供不需要的 property array 即可,適合大部分 property 都要抽取的場合。
omit()
[String] → {String: *} → {String: *}
從一個 object 中忽略部分 property 成為新 object
[string]
:要忽略的 property
{String: *}
:data 為 object
{Strig: *}
:回傳新的 object
Conclusion
-
pick()
與omit()
為互補,可視需求靈活運用
Reference
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。