Open In App

Lodash _.takeRight() Method

Last Updated : 02 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Lodash _.takeRight() method is used to get the array of n elements from the end of the given array.

Syntax:

_.takeRight(array, n);

Parameters:

  • array: This parameter holds the query array.
  • n: This parameter holds the number of elements.

Return Value:

  • This method returns an array containing n elements from the given end.

Example 1: In this example, we are getting an array that has only one element.

javascript
const _ = require('lodash');

let x = [1, 2, 3, 4, 5, 6, 7]
let newArray = _.takeRight(x);

console.log(newArray);

Output:

[7]

Example 2: In this example, we are getting an array that has five elements.

javascript
const _ = require('lodash');

let x = [1, 2, 3, 4, 5, 6, 7]
let newArray = _.takeRight(x, 5);

console.log(newArray);

Output:

[ 3, 4, 5, 6, 7 ]

Example 3: In this example, we are getting an array that has no element.

javascript
const _ = require('lodash');

let x = [1, 2, 3, 4, 5, 6, 7]
let newArray = _.takeRight(x, 0);

console.log(newArray);

Output:

[]

Note: This will not work in normal JavaScript because it requires the library lodash to be installed.


Explore