How to trim the array if length is > 5

后端 未结 2 390
失恋的感觉
失恋的感觉 2021-01-27 04:25

How to trim the array if length is > 5

My JSON is:

{
        \"name\": \"aaa\"
        \"files\": [
            {
                \"name\": \"A\",
               


        
相关标签:
2条回答
  • 2021-01-27 05:04

    I suppose you want to keep the full array in your logic, and only trim it in your HTML. If this is the case, just use Array.prototype.slice() where you want to display the trimmed list.

    I.e.

    <div *ngFor="let i of arr.slice(0,5)">
      {{i}}
    </div>
    
    0 讨论(0)
  • 2021-01-27 05:24

    You can use slice for that.

    let trimmed = data.titles.slice(0, 5);
    
    0 讨论(0)
提交回复
热议问题