Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip first in map function

I want to skip the first in the .map function what I do now is this:

block.gallery.map((item, i) => (
       <div className="gallery-item" key={i}>
        { block.gallery.length > 4 && i !== 0 ?
          <div className="inner">
             <img src={block.gallery[i].images.thumbnail_sm} alt={block.title} srcSet={`${block.gallery[i].images.thumbnail_md} 1x, ${block.gallery[i].images.thumbnail_lg} 2x`} className="img-fluid" title="" />
             <div className="img-overlay">
               <span className="img-overlay-body">{ block.gallery.length - 4 }+ <span className="hidden-xs">Foto's</span></span>
             </div>
           </div>
           :
           <img src="http://placehold.it/800x538" alt="" className="img-fluid" title="" />
          }
like image 283
Sireini Avatar asked Nov 18 '16 14:11

Sireini


People also ask

Can we break a map function?

To break a map() loop 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.

Can we break map loop?

Not possible, We can't break #array. map, it will run for each element of array.

Can we use continue in map Javascript?

You can't, but you could filter out all null values afterwards.


2 Answers

So slice it to skip the first

block.gallery.slice(1).map(...)

You also need to reference the item passed into map, not using the index with the orginal array.

block.gallery[i].images.thumbnail_sm

should be

item.images.thumbnail_sm
like image 138
epascarello Avatar answered Sep 20 '22 20:09

epascarello


You can also remove the first element, and retrieve it at any point, using Array.prototype.shift(); however, this modifies the original array.

const head = block.gallery.shift(); // removes and stores first element in "head"

console.log(block.gallery) // array with first element removed
like image 27
darcher Avatar answered Sep 22 '22 20:09

darcher