VOOZH about

URL: https://www.geeksforgeeks.org/javascript/lodash-_-take-method/

⇱ Lodash _.take() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Lodash _.take() Method

Last Updated : 2 Sep, 2024

Lodash _.take() method is used to create a slice of an array with n elements from the beginning.

Syntax:

_.take(array, n);

Parameters:

  • array: This parameter holds the query array.
  • n: This parameter holds the number that represents the number of elements to take from the array.

Return Value:

  • This method returns an array of first n elements.

Example 1: In this example, we are creating a new array having 3 elements of an old array by the use of the lodash _.take() method.

Output:

[ 1, 2, 'a' ]

Example 2: In this example, we are creating a new array having 2 elements of an old array by the use of the lodash _.take() method.

Output:

[ { name: 'lodash' }, { name: 'npm' } ]

Example 3: In this example, we are creating a new array having 5 elements of an old array by the use of the lodash _.take() method.

Output:

[ 1, 2, 3, 4, 5 ]

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

Comment