Ramda 之 remove()

栏目: JavaScript · 发布时间: 6年前

内容简介:若要從 Array 擷取其中一部分,但我們無法如假如我們一開始不知道 Ramda 的

若要從 Array 擷取其中一部分,但我們無法如 slice() 指定要的部分,只能指定不要的部分,此時我們可使用 Ramda 的 remove()

Functional

import { slice, without } from 'ramda';

let data = [1, 2, 3, 4, 5];

let remove = (start, count, arr) => without(slice(start, start + count, arr), arr);
console.dir(remove(1, 3, data));

假如我們一開始不知道 Ramda 的 remove() ,也可透過 slice()without() 組合:

slice()
without()

Ramda 之 remove()

remove()

Value

import { remove } from 'ramda';

let data = [1, 2, 3, 4, 5];
console.dir(remove(1, 3, data));

事實上 Ramda 已經內建 remove() ,可直接使用。

remove()

Number → Number → [a] → [a]

直接指定 Array 中不要的部分擷取部分 Array

Number :Array 的 start index

Number :Array 的 element count

[a] :Data 為 array

[a] :所截取的 array

Object

import { remove } from 'ramda';

let data = [
  { firstName: 'Sam',    lastName: 'Xiao'},
  { firstName: 'Kevin',  lastName: 'Yang'},
  { firstName: 'Jimmy',  lastName: 'Huang'},
  { firstName: 'Jessie', lastName: 'Hsieh'},
  { firstName: 'John',   lastName: 'Wu'}
];

console.dir(remove(1, 3, data));

若 element 為 object, remove() 依然適用。

Ramda 之 remove()

Conclusion

  • remove() 是與 slice() 相對的 function: slice() 是指定要的部分,而 remove() 是指定不要的部分

Reference

Ramda , remove()

Ramda , slice()

Ramda , without()


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

查看所有标签

猜你喜欢:

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

The Everything Store

The Everything Store

Brad Stone / Little, Brown and Company / 2013-10-22 / USD 28.00

The definitive story of Amazon.com, one of the most successful companies in the world, and of its driven, brilliant founder, Jeff Bezos. Amazon.com started off delivering books through the mail. Bu......一起来看看 《The Everything Store》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具