Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling .map on the first X elements of an array

I want to call .map on only the first X elements of an array. How do I do this?

like image 551
user730569 Avatar asked May 09 '11 03:05

user730569


People also ask

Can you call map on an array?

map() calls a function once for each element in an array. map() does not execute the function for empty elements. map() does not change the original array.

How do you map part of an array?

To map() only a portion of an array in React: Call the slice() method on the array to get a portion of the array. Call the map() method on the portion of the array. Iterate over the portion of the array.

How can you access the first element of the array?

Using reset() function: The reset() function is used to move the array's internal pointer to the first element. Example: This example illustrates the use of the reset() function that helps to move any array's internal pointer to the first element of that array.

What is the correct way to read the value of the first element in an array in JavaScript?

The first and last elements are accessed using an index and the first value is accessed using index 0 and the last element can be accessed through length property which has one more value than the highest array index. The array length property in JavaScript is used to set or return the number of elements in an array.


1 Answers

In ruby 1.9

array.first(X).map{|e| ...}

In ruby 1.8

array[0, X].map{|e| ...}
like image 85
sawa Avatar answered Oct 26 '22 15:10

sawa