💧 Skips a specified number of elements from the start of an array.

Info

It is loosely based on the Iterator.prototype.drop() method.

Syntax

TypeScript
import { drop } from '@opentf/std';

drop<T>(
  arr: T[],
  limit: number | null = 1,
  cb?: (val: T) => boolean,
  right?: boolean
): T[]

Parameters

  • arr: The source array.

  • limit: The number of elements to drop. If null, all matching elements (or all elements if no callback) are dropped.

  • cb: An optional predicate function. If provided, only elements that return true are counted towards the limit.

  • right: If true, drops from the end of the array instead of the start. Default: false.

Returns

A new array with the elements dropped.

Examples

TypeScript
drop([1, 2, 3], -1) // Throws RangeError for negative limit

drop([1, 2, 3], 0) //=> [1, 2, 3]

drop([1, 2, 3]) //=> [2, 3]

drop([1, 2, 3], 1) //=> [2, 3]

drop([1, 2, 3], 2) //=> [3]

drop([1, 2, 3, 4, 5], 5) //=> []

drop([1, 2, 3, 4, 5], 6) //=> []

drop([1, 2, 3, 4, 5], null) //=> []

drop([1, 2, 3, 4, 5], 2, (val) => val % 2 === 0) //=> [1, 3, 5]

drop([1, 2, 3, 4, 5], 3, (val) => val % 2 !== 0) //=> [2, 4]

drop([1, 2, 3, 4, 5], 3, undefined, true) //=> [1, 2]

const users = [
  { name: 'x', active: false },
  { name: 'y', active: true },
  { name: 'z', active: false },
];
drop(users, null, (val) => val.active) 
//=> [
//   { name: 'x', active: false },
//   { name: 'z', active: false },
// ]
Last updated on
Edit this page