HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptCritical

How to get first N number of elements from an array

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
arraygethowfromnumberelementsfirst

Problem

I am working with Javascript(ES6) /FaceBook react and trying to get the first 3 elements of an array that varies in size. I would like do the equivalent of Linq take(n).

In my Jsx file I have the following:

var items = list.map(i => {
  return (
    
  );
});


Then to get the first 3 items I tried

var map = new Map(list);
    map.size = 3;
    var items = map(i => {
      return ();
    });


This didn't work as map doesn't have a set function. What can I try next?

Solution

I believe what you're looking for is:

// ...inside the render() function

var size = 3;
var items = list.slice(0, size).map(i => {
    return 
});                       
return (
  
    {items}
     
)

Code Snippets

// ...inside the render() function

var size = 3;
var items = list.slice(0, size).map(i => {
    return <myview item={i} key={i.id} />
});                       
return (
  <div>
    {items}
  </div>   
)

Context

Stack Overflow Q#34883068, score: 870

Revisions (0)

No revisions yet.