CyrefJS
CCyrefJS
فا
Back to documentation
Array/Since 1.38.0

dropRight

Returns a new array with the last n elements removed.

dropRight(array, n):T[]

Parameters

arrayT[]required

The array from which elements should be removed.

nnumberrequired

The number of elements to remove from the end of the array.

Returns

T[]

A new array containing the elements before the last n elements.

Examples

01

Basic usage

Remove the last two elements from an array.

TypeScript
import { dropRight } from "@cyref/js";

const numbers = [1, 2, 3, 4, 5];

const result = dropRight(numbers, 2);

console.log(result);
// [1, 2, 3]
02

Drop more elements than the array contains

If n is greater than or equal to the array length, an empty array is returned.

TypeScript
import { dropRight } from "@cyref/js";

const numbers = [1, 2, 3];

const result = dropRight(numbers, 5);

console.log(result);
// []
03

Zero or negative n

If n is less than or equal to 0, the function returns a copy of the original array.

TypeScript
import { dropRight } from "@cyref/js";

const numbers = [1, 2, 3];

const result = dropRight(numbers, 0);

console.log(result);
// [1, 2, 3]
04

Drop all elements

When n equals the array length, the function returns an empty array.

TypeScript
import { dropRight } from "@cyref/js";

const numbers = [1, 2, 3];

const result = dropRight(numbers, 3);

console.log(result);
// []
Built with@cyref/js·Official documentation