Ramda 之 omit()

栏目: 编程语言 · 发布时间: 6年前

内容简介:若要抽取 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

Ramda 之 omit()

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

Ramda 之 omit()

Conclusion

  • pick()omit() 為互補,可視需求靈活運用

Reference

Ramda , pick()

Ramda , omit()


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

匠心体验

匠心体验

[法] 艾米丽·布歇 / 吴 博 / 人民邮电出版社 / 2016-11 / 69.00元

本书针对在智能手机和平板电脑的网站及应用程序设计,详细剖析了移动终端服务的用户体验设计要点,阐述了营造舒适的感官体验、甄选内容及功能、提高用户效率、优化等待时间、合理实施教学、情感设计等方面的设计诀窍,并通过大量实例,呈现当今移动终端服务设计中的亮点与雷区。一起来看看 《匠心体验》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

html转js在线工具
html转js在线工具

html转js在线工具