Array
drop

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

It is loosely based on the Iterator.prototype.drop() (opens in a new tab) method.

Syntax

import { drop } from '@opentf/std';
 
drop<T>(
  arr: T[],
  limit: number | null = 1,
  cb?: (val: T) => 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.

Returns

A new array with the elements dropped.

Examples

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]
 
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 },
// ]

Related

Try