javascript - How to remove separator icon for last array map? - Stack Overflow

I have an array of strings that I want to render in a way that's one after the other with arrows i

I have an array of strings that I want to render in a way that's one after the other with arrows in between as separators.

const folders = ["Folder 1", "Folder 2"]

So I tried the following code :

<div style={{display: "flex"}}>
   {folders.map(folder => (
     <p><i className='fa fa-folder'></i> {folder} <i className="fa fa-chevron-right"></i></p>
   ))}
</div>

What this does it put that arrow icon after every folder but i don't want it to put that arrow after the last folder. How do I fix?

I have an array of strings that I want to render in a way that's one after the other with arrows in between as separators.

const folders = ["Folder 1", "Folder 2"]

So I tried the following code :

<div style={{display: "flex"}}>
   {folders.map(folder => (
     <p><i className='fa fa-folder'></i> {folder} <i className="fa fa-chevron-right"></i></p>
   ))}
</div>

What this does it put that arrow icon after every folder but i don't want it to put that arrow after the last folder. How do I fix?

Share Improve this question asked Aug 2, 2021 at 6:36 user16516348user16516348 1179 bronze badges 1
  • Map can take a second argument for index. folders.map((folder, index) =>... use the index to check if it is the last element to skip adding the arrow icon. – Coding Otaku Commented Aug 2, 2021 at 6:43
Add a ment  | 

4 Answers 4

Reset to default 5

With Array.prototype.map you can have access to the index of item of the array in which you are performing a map

<div style={{display: "flex"}}>
    {folders.map((folder, index) => {
        return (
            <p key={index}>
                <i className='fa fa-folder'></i> 
                {folder} 
                {index !== folders.length - 1 ? <i className="fa fa-chevron-right"></i>: null}
            </p>
         )
        );
    })}
</div>

As you can see I add a line which check if the current index in the map is equal to the length of the folders and if is not the case the closing chevron is display otherwise it is not display.

Try:

<div style={{display: "flex"}}>
  {folders.map((folder, key) => (
   <p><i className='fa fa-folder'></i> {folder} 
   {index !== (folders.length - 1) ?  <i className="fa fa-chevron-right"></i> : ''} </p>
 ))}
</div>

You can use the index parameter of the map function e.g.

<div style={{display: "flex"}}>
   {folders.map((folder, index) => (
     <p><i className='fa fa-folder'></i> {folder} 
     {index < folders.length -1 && <i className="fa fa-chevron-right"></i>}
</p>
   ))}
</div>

Make use of index and array length.

<div style={{display: "flex"}}>
   {folders.map((folder, index) => (
     <p><i className='fa fa-folder'></i> {folder} {folders.length-1 !== index && <i className="fa fa-chevron-right"></i>}</p>
   ))}
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745563194a4633231.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信